jQuery API jQuery API 中英文对照版 - jQuery在线查询手册
width(val)
width(val)

为每个匹配的元素设置CSS宽度(width)属性的值。如果没有明确指定单位(如:em或%),使用px。

返回值:jQuery

参数:

  • val (String|Number): 要设置的宽度值
 

示例:

$("p").width(20);

HTML 代码:

<p>This is just a test.</p>

结果:

<p style="width:20px;">This is just a test.</p>

示例:

$("p").width("20em");

HTML 代码:

<p>This is just a test.</p>

结果:

<p style="width:20em;">This is just a test.</p>

 
width( val )

Set the CSS width of every matched element. If no explicit unit was specified (like 'em' or '%') then "px" is added to the width.

Return value: jQuery
Parameters:

  • val (String|Number): Set the CSS property to the specified value.
 

Example:

 $("p").width(20);  

Before:

 <p>This is just a test.</p>  
Result:
 <p style="width:20px;">This is just a test.</p>

Example:

 $("p").width("20em");  
Before:
 <p>This is just a test.</p>  

Result:

 <p style="width:20em;">This is just a test.</p>