jQuery css()メソッド

❮jQueryHTML/ CSSメソッド

すべての<p>要素のcolorプロパティを設定します。

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

定義と使用法

css()メソッドは、選択した要素の1つ以上のスタイルプロパティを設定または返します。

プロパティを返すために使用される場合:
このメソッドは、最初に一致した要素の指定されたCSSプロパティ値を返します。ただし、省略形のCSSプロパティ(「background」や「border」など)は完全にはサポートされておらず、ブラウザによって結果が異なる場合があります。

プロパティの設定に使用する場合:
このメソッドは、一致するすべての要素に指定されたCSSプロパティを設定します。


構文

CSSプロパティ値を返します。

$(selector).css(property)

CSSのプロパティと値を設定します。

$(selector).css(property,value)

関数を使用してCSSプロパティと値を設定します。

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

複数のプロパティと値を設定します。

$(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

自分で試してみてください-例


返す最初に一致した要素の指定されたCSSプロパティ値を返します。


を使用して、選択した要素のCSSプロパティを変更します。


を設定する選択した要素に複数のCSSプロパティと値を設定する方法。


❮jQueryHTML/ CSSメソッド