一、收集例子
1、如需包含 margin,请使用 outerHeight(true)
<style>
.test{
height:100px;
width:200px;
padding:10px;
margin:10px;
border:5px solid red;
}
</style>
<div class="test"></div>
<script>
$(function(){
var h = $(".test").outerHeight(true)
console.log(h) // 150
// padding + border + element + margin (true)
})
</scrpit>
2、返回第一个匹配元素的外高度。
<style>
#p1 {
color:red;
height:50px;
border:5px solid;
}
#p2 {
background:yellow;
height:60px;
border:5px solid;
}
</style>
<body>
<p id="p1" class="test">你好</p>
<p id="p2" class="test">你好</p>
</body>
<script>
$(document).ready(function() {
var h = $(".test").outerHeight();
console.log(h) // 60
});
</script>
3.outerHeight()不适用于window和document,height() == outerHeight()
var docO = $(document).outerHeight();
var docH = $(document).height();
var winO = $(window).outerHeight();
var winH = $(window).height();
console.log(docO,docH,winO,winH) // 280
二、outerHeight是什么
outerHeight 是一个返回Number类型的方法,
参数是bool类型,true包含margin的值,默认参数为 false ,不包含margin的值
outerHeight 只返回第一个匹配的元素的外部高度