jQuery css() Metode

Metode HTML/CSS jQuery

Contoh

Setel properti warna dari semua elemen <p>:

$("button").click(function(){
  $("p").css("color", "red");
});

Definisi dan Penggunaan

Metode css() menyetel atau mengembalikan satu atau lebih properti gaya untuk elemen yang dipilih.

Saat digunakan untuk mengembalikan properti:
Metode ini mengembalikan nilai properti CSS yang ditentukan dari elemen yang cocok PERTAMA. Namun, properti CSS singkatan (seperti "latar belakang" dan "batas") tidak sepenuhnya didukung dan dapat memberikan hasil yang berbeda di browser yang berbeda.

Saat digunakan untuk menyetel properti:
Metode ini menyetel properti CSS yang ditentukan untuk SEMUA elemen yang cocok.


Sintaksis

Kembalikan nilai properti CSS:

$(selector).css(property)

Setel properti dan nilai CSS:

$(selector).css(property,value)

Setel properti dan nilai CSS menggunakan fungsi:

$(selector).css(property,function(index,currentvalue))

Tetapkan beberapa properti dan nilai:

$(selector).css({property:value, property:value, ...})

Parameter Description
property Specifies the CSS property name, like "color", "font-weight", etc.
value Specifies the value of the CSS property, like "red", "bold", etc.
function(index,currentvalue) Specifies a function that returns the new value for the CSS property
  • index - Returns the index position of the element in the set
  • currentvalue - Returns the current value of the CSS property

Cobalah Sendiri - Contoh


Mengembalikan nilai properti CSS yang ditentukan dari elemen yang cocok PERTAMA.


Menggunakan fungsi untuk mengubah properti CSS untuk elemen yang dipilih.


Cara mengatur beberapa properti dan nilai CSS untuk elemen yang dipilih.


Metode HTML/CSS jQuery