📜  jQuery css()

📅  最后修改于: 2020-11-26 00:42:16             🧑  作者: Mango

jQuery css()

jQuery CSS()方法用于获取(返回)或设置所选元素的样式属性或值。它有助于您获得一个或多个样式属性。

jQuery CSS()方法提供两种方法:

1)返回一个CSS属性

它用于获取指定CSS属性的值。

句法:

css("propertyname");

让我们以一个示例来演示此属性。








This is a heading

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.

输出:

This is a heading

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.


注意:上面的示例返回第一个匹配元素的背景色值。

2)设置CSS属性

此属性用于为所有匹配的元素设置特定的值。

句法:

css("propertyname","value");   







The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.

This paragraph has no background-color.

输出:

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.

3)设置多个CSS属性

它只是Set CSS属性的扩展。它有助于您将多个属性值添加在一起。

句法:

css({"propertyname":"value","propertyname":"value",...});  

让我们以一个示例来演示此属性。在此示例中,我们为所有元素添加两个属性background-color和font-size。








This is a heading

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.

This paragraph has no background-color.

输出:

The background-color of this paragraph is red.

The background-color of this paragraph is green.

The background-color of this paragraph is blue.