考虑到内部元素的利润率,如何获得DIV的高度? [英] How to get the height of a DIV considering inner element's margins?

查看:97
本文介绍了考虑到内部元素的利润率,如何获得DIV的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下标记:

 < div id =outerElement> 
< div id =innerElementstyle =border:1px solid blue; background-color:#f0f3f5; margin-top:100px>
TESTE
< / div>
< / div>

我想获得 outerElement 使用javascript 。我注意到,如果我从 innerElement 删除垂直边距,我可以得到我想要的但我不能改变 outerElement



我该如何操作?


我已经在所有浏览器中尝试了height,scrollheight和offsetHeight。 Chrome为我提供了 scrollHeight 的预期值(包括内部元素的边距)。

解决方案

添加clearfix,使用jquery height()并再次移除clearfix类。 b
$ b

这会奏效。 :)

Consider de following markup:

<div id="outerElement">
    <div id="innerElement" style="border: 1px solid blue; background-color: #f0f3f5; margin-top: 100px">  
        TESTE
    </div>
</div>

I want to get the actual final height of outerElement using javascript. I noticed that if I remove the vertical margins from innerElement I am able to get what I want but I cannot alter styles from within the outerElement.

How do I do this?

Obs: I already tried height, scrollheight and offsetHeight in all browsers. Chrome gives me the expected value (including inner element's margins) for scrollHeight. All other browsers fail.

解决方案

Add clearfix, use jquery height() and remove the clearfix class again.

That's gonna work. : )

这篇关于考虑到内部元素的利润率,如何获得DIV的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆