.width()


取得第一個匹配元素集合中元素的目前計算寬度,或設定每個匹配元素的寬度。

.width()傳回:數字

說明: 取得第一個匹配元素集合中元素的目前計算寬度。

  • 新增版本:1.0.width()

    • 此方法不接受任何參數。

.css( "width" ).width() 的差異在於,後者傳回一個沒有單位的像素值(例如,400),而前者傳回一個具有完整單位的數值(例如,400px)。當元素的寬度需要用於數學計算時,建議使用 .width() 方法。

圖 1 - 測量寬度的說明

此方法也可以找出視窗和文件的寬度。

1
2
3
4
5
// Returns width of browser viewport
$( window ).width();
// Returns width of HTML document
$( document ).width();

請注意,.width() 將永遠傳回內容寬度,而不論 CSS box-sizing 屬性的值為何。從 jQuery 1.8 開始,這可能需要擷取 CSS 寬度加上 box-sizing 屬性,然後在元素具有 box-sizing: border-box 時減去每個元素的任何潛在邊框和內距。若要避免此懲罰,請使用 .css( "width" ) 而不是 .width()

注意:儘管 stylescript 標籤在絕對定位且給定 display:block 時會報告 .width()height() 的值,但強烈建議不要對這些標籤呼叫這些方法。除了這是一個不良做法之外,結果也可能證明不可靠。

其他注意事項

  • 在某些情況下,由與尺寸相關的 API 傳回的數字,包括 .width(),可能是小數。程式碼不應假設它是整數。此外,當使用者縮放頁面時,尺寸可能不正確;瀏覽器不會公開 API 來偵測此狀況。
  • 當元素或其父項隱藏時,.width() 報告的值無法保證準確。若要取得準確的值,請確保元素在使用 .width() 之前可見。jQuery 會嘗試暫時顯示然後重新隱藏元素以測量其尺寸,但這不可靠,而且(即使準確)也會顯著影響頁面效能。此顯示和重新隱藏測量功能可能會在未來的 jQuery 版本中移除。

範例

顯示各種寬度。請注意,這些值來自 iframe,因此可能比您預期的還要小。黃色亮顯顯示 iframe 主體。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>width demo</title>
<style>
body {
background: yellow;
}
button {
font-size: 12px;
margin: 2px;
}
p {
width: 150px;
border: 1px red solid;
}
div {
color: red;
font-weight: bold;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<button id="getp">Get Paragraph Width</button>
<button id="getd">Get Document Width</button>
<button id="getw">Get Window Width</button>
<div>&nbsp;</div>
<p>
Sample paragraph to test width
</p>
<script>
function showWidth( ele, w ) {
$( "div" ).text( "The width for the " + ele + " is " + w + "px." );
}
$( "#getp" ).on( "click", function() {
showWidth( "paragraph", $( "p" ).width() );
} );
$( "#getd" ).on( "click", function() {
showWidth( "document", $( document ).width() );
} );
$("#getw").on( "click", function() {
showWidth( "window", $( window ).width() );
} );
</script>
</body>
</html>

示範

.width( value )傳回: jQuery

說明: 設定匹配元素集合中每個元素的 CSS 寬度。

  • 版本新增:1.0.width( 值 )

    • 類型:字串數字
      表示像素數的整數,或整數加上一個可選的度量單位(作為字串)。
  • 版本新增:1.4.1.width( 函式 )

    • 函式
      類型:函式( 整數 索引, 整數 值 ) => 字串數字
      傳回要設定的寬度的函式。接收集合中元素的索引位置和舊寬度作為參數。在函式中,this 參照集合中目前的元素。

呼叫 .width("value") 時,值可以是字串(數字和單位)或數字。如果只提供數字作為值,jQuery 會假設是像素單位。但是,如果提供字串,任何有效的 CSS 度量值都可以用於寬度(例如 100px50%auto)。請注意,在現代瀏覽器中,CSS 寬度屬性不包括內邊距、邊框或外邊距,除非使用 box-sizing CSS 屬性。

如果未指定明確的單位(例如「em」或「%」),則假設為「px」。

請注意,.width("value") 會設定方塊的內容寬度,而不論 CSS box-sizing 屬性的值為何。

範例

在第一次按一下時變更每個 div 的寬度(並變更其顏色)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>width demo</title>
<style>
div {
width: 70px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<div>d</div>
<div>d</div>
<div>d</div>
<div>d</div>
<div>d</div>
<script>
var modWidth = 50;
$( "div" ).one( "click", function() {
$( this ).width( modWidth ).addClass( "mod" );
modWidth -= 8;
});
</script>
</body>
</html>

示範