.height()


取得第一個符合元素集合的計算高度,或設定每個符合元素的高度。

.height()傳回: 數字

說明: 取得第一個符合元素集合的計算高度。

  • 新增版本: 1.0.height()

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

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

圖 1 - 測量高度的說明

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

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

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

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

其他注意事項

  • 包括 .height() 在內的與尺寸相關的 API 所傳回的數字在某些情況下可能是小數。程式碼不應假設它是一個整數。此外,當使用者縮放頁面時,尺寸可能不正確;瀏覽器不會公開 API 來偵測此條件。
  • 當元素或其父元素被隱藏時,.height() 回報的值無法保證準確性。若要取得準確的值,請確保元素在使用 .height() 之前是可見的。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
52
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>height 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 Height</button>
<button id="getd">Get Document Height</button>
<button id="getw">Get Window Height</button>
<div>&nbsp;</div>
<p>
Sample paragraph to test height
</p>
<script>
function showHeight( element, height ) {
$( "div" ).text( "The height for the " + element + " is " + height + "px." );
}
$( "#getp" ).on( "click", function() {
showHeight( "paragraph", $( "p" ).height() );
});
$( "#getd" ).on( "click", function() {
showHeight( "document", $( document ).height() );
});
$( "#getw" ).on( "click", function() {
showHeight( "window", $( window ).height() );
});
</script>
</body>
</html>

示範

.height( value )傳回:jQuery

說明:設定每個符合的元素的 CSS 高度。

呼叫 .height(value) 時,值可以是字串(數字和單位)或數字。如果只提供數字作為值,jQuery 會假設為像素單位。但是,如果提供字串,則必須提供有效的 CSS 度量值作為高度(例如 100px50%auto)。請注意,在現代瀏覽器中,CSS 高度屬性不包括內距、邊框或外距。

如果未指定明確的單位(例如「em」或「%」),則會將「px」連結到值。

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

範例

設定每個 div 在按一下時的高度為 30px,並變更顏色。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>height demo</title>
<style>
div {
width: 50px;
height: 70px;
float: left;
margin: 5px;
background: rgb(255,140,0);
cursor: pointer;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<script>
$( "div" ).one( "click", function() {
$( this ).height( 30 ).css({
cursor: "auto",
backgroundColor: "green"
});
});
</script>
</body>
</html>

示範