【深入分析的近义词】深入分析jQuery height()、innerHeight()、outerHeight()函数的区别

更新时间:2019-11-13    来源:jquery    手机版     字体:

【www.bbyears.com--jquery】


在jQuery中,获取元素高度的函数有3个,它们分别是height()、 innerHeight()、 outerHeight()。

与此相对应的是,获取元素宽度的函数也有3个,它们分别是width()、 innerWidth()、 outerWidth()。

在这里,我们以height()、innerHeight()、outerHeight()3个函数为例,来详细介绍它们之间的区别。

下面我们以元素element的盒模型为例来介绍它们之间的区别。

css-box-model-diagram.pngjquery183017558297083482371="72" src="http://filesimg.111cn.net/2015/06/09/20150609144356383.png" />

函数 高度范围 jQuery版本 支持写操作

height() height 1.0+
 1.0+
innerHeight() height + padding 1.2.6+ 1.8.0+
outerHeight() height + padding + border 1.2.6+ 否
outerHeight(true) height+padding+border+margin 1.2.6+ 否

1、 只有height()函数可用于window或document对象。
2、 "支持写操作"表示该函数可以为元素设置高度值。
3、 1.4.1+ height()新增支持参数为函数(之前只支持数值)。
4、 1.8.0+ innerHeight()支持参数为数值或函数。



 
 
<script type="text/javascript">
var $ele = $("#element");
 
// height() = height(100) = 100
document.writeln( $ele.height() ); // 100
 
// innerHeight() = height(100) + padding(10*2)= 120 
document.writeln( $ele.innerHeight() ); // 120
 
// outerHeight() = height(100) + padding(10*2) + border(1*2) = 122 
document.writeln( $ele.outerHeight() ); // 122
 
// outerHeight(true) = height(100) + padding(10*2) + border(1*2) + margin(5*2) = 132 
document.writeln( $ele.outerHeight(true) ); // 132
</script>

补充:

标准浏览器下:

height:高度

innerHeight:高度+补白
outerHeight:高度+补白+边框,参数为true时:高度+补白+边框+边距

html代码:

jjjjj

js代码:

alert($("#test").height());
alert($("#test").innerHeight());
alert($("#test").outerHeight());
alert($("#test").outerHeight(true));

结果:
在ie中的结果:17px,37px,41px,61px
在ff中的结果:20px,40px,44px,64px

html代码:

jjjjj

js代码:

alert($("#test").height());
alert($("#test").innerHeight());
alert($("#test").outerHeight());
alert($("#test").outerHeight(true));
[html]
结果:
在ie中的结果:17px,37px,41px,61px
在ff中的结果:41px,61px,65px,85px

html代码:
[code]
jjjjj

js代码:

alert($("#test").height());
alert($("#test").innerHeight());
alert($("#test").outerHeight());
alert($("#test").outerHeight(true));

结果:
在ie中的结果:18px,38px,42px,62px
在ff中的结果:42px,62px,66px,86px

html代码:

jjjjj

js代码:

alert($("#test").height());
alert($("#test").innerHeight());
alert($("#test").outerHeight());
alert($("#test").outerHeight(true));

结果:
在ie中的结果:36px,56px,60px,80px
在ff中的结果:60px,80px,84px,104px

本文来源:http://www.bbyears.com/wangyezhizuo/78897.html

热门标签

更多>>

本类排行