jQuery API 返回首页目录 | jQuery API 中英文对照版
css(key, value)
css(key, value)

在所有匹配的元素中,设置一个样式属性的值。

返回值:jQuery

参数:

  • key (String): 要设置的样式属性名称
  • value (Object): 要设置的样式属性的值
 
示例:

把所有段落的color样式属性值改为红色。

$("p").css("color","red");

HTML 代码:

<p>Test Paragraph.</p>

结果:

<p style="color:red;">Test Paragraph.</p>

示例:

把所有段落的left样式属性值设置为"30px"

$("p").css("left",30);

HTML 代码:

<p>Test Paragraph.</p>

结果:

<p style="left:30px;">Test Paragraph.</p>

 
css( key, value )

Set a single style property to a value, on all matched elements. If a number is provided, it is automatically converted into a pixel value.

Return value: jQuery
Parameters:

  • key (String): The name of the property to set.
  • value (String|Number): The value to set the property to.
 

Example:

Changes the color of all paragraphs to red

 $("p").css("color","red");  
Before:
 <p>Test Paragraph.</p>  

Result:

 <p style="color:red;">Test Paragraph.</p>  


Example:

Changes the left of all paragraphs to "30px"

 $("p").css("left",30);  
Before:
 <p>Test Paragraph.</p>  
Result:
 <p style="left:30px;">Test Paragraph.</p>  
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog