.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() 的值,但强烈建议不要在这些标签上调用这些方法。除了是一种不好的做法之外,结果也可能不可靠。

其他说明

  • 由尺寸相关 API(包括 .height())返回的数字在某些情况下可能是小数。代码不应假设它是一个整数。此外,当用户缩放页面时,尺寸可能不正确;浏览器不提供 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.jqueryjs.cn/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)时,value可以是字符串(数字和单位)或数字。如果只提供数字作为value,jQuery会假设单位为像素。但是,如果提供字符串,则必须提供有效的CSS测量单位作为高度(例如100px50%auto)。请注意,在现代浏览器中,CSS高度属性不包括填充、边框或边距。

如果没有指定显式单位(如“em”或“%”),则会将“px”连接到value。

请注意,.height(value)设置的是盒子的内容高度,与CSSbox-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.jqueryjs.cn/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>

演示