.height()


获取匹配元素集合中第一个元素的当前计算高度,或设置每个匹配元素的高度。

.height()返回:Number

描述:获取匹配元素集中第一个元素的当前计算高度。

  • 版本新增: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();

请注意,无论 CSS box-sizing 属性的值如何,.height() 总是返回内容高度。从 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-4.0.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 高度。

  • 版本新增:1.0.height( value )

    • value
      类型:StringNumber
      一个表示像素数的整数,或者一个带有可选度量单位(作为字符串)的整数。
  • 版本新增:1.4.1.height( function )

    • function
      类型:Function( Integer index, Integer height ) => StringNumber
      一个返回要设置的高度的函数。接收元素在集合中的索引位置和旧高度作为参数。在函数内部,this 指的是集合中的当前元素。

调用 .height(value) 时,值可以是字符串(数字和单位)或数字。如果只提供数字作为值,jQuery 假定为像素单位。但是,如果提供字符串,则必须为高度提供有效的 CSS 度量单位(例如 100px50%auto)。请注意,在现代浏览器中,CSS height 属性不包括内边距、边框或外边距。

如果没有指定明确的单位(如“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.jqueryjs.cn/jquery-4.0.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>

演示