jQuery 获取元素的高度

  • jQuery 中,获取元素高度的方法有 3 个:height()innerHeight()outerHeight()

  • 元素的盒模型: height(高度)padding(内边距)margin(外边距)border(边框)

  • height()

    用于设置或返回当前匹配元素的高度;
    高度不包括元素的外边距(margin)、内边距(padding)、边框(border)等;
    对不可见的元素依然有效;
    还可获取 window、document 对象的高度:
    $(window).height();   // 返回浏览器视口的高度
    $(document).height(); // 返回 HTML 文档的高度
    
  • innerHeight()

    用于设置或返回当前匹配元素的内高度;
    内高度包括只元素的内边距(padding);
    对不可见的元素依然有效;
    不适用于获取 window、document 对象的高度;
    
  • outerHeight()

    获取当前匹配元素的外高度;
    外高度包括元素的内边距(padding)、边框(border);
    outerHeight(true) 参数为 true 时包含外边距部分的高度,默认为 false 不包括;
    对不可见的元素依然有效;
    不适用于获取 window、document 对象的高度;
    
  • 总结

    height()          高度为 height
    innerHeight()     高度为 height+padding
    outerHeight()     高度为 height+padding+border
    outerHeight(true) 高度为 height+margin+padding+border
    
  • 举例

    <div style="margin:5px; padding:10px; width:100px; height:100px; border:1px solid #666;"></div>
    
    <script type="text/javascript">
        var div=$("div");
        console.log(div.height());          // 输出 100
        console.log(div.innerHeight());     // 输出 100+10+10=120
        console.log(div.outerHeight());     // 输出 100+10+10+1+1=122
        console.log(div.outerHeight(true)); // 输出 100+10+10+1+1+5+5=132
    </script>
    
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡尔特斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值