简体   繁体   English

计算div的高度

[英]Calculate height of div

I have a div with a set height. 我有一个设定高度的div。

<div class="foo" style="height:100px;">bar</div>

Is it possible to find what the div's height would be if the height was not explicitly set by my style property? 如果没有由我的style属性明确设置高度,是否有可能找到div的高度?

In other words, I'd like to find the height of the following div without actually changing the div. 换句话说,我想找到以下div的高度,而无需实际更改div。

<div class="foo">bar</div>
var clone = $('.foo').clone();
clone.css('height', 'auto');
clone.css('visibility', 'hidden');

$('body').append(clone);

console.log(clone.height());

Use the clientHeight property to get the inner height of the element. 使用clientHeight属性获取元素的内部高度。 clientHeight is equivalent to the css height property. clientHeight等效于css height属性。 See the snippet below (the first line demonstrates the use of clientHeight ): 请参见下面的代码片段(第一行演示了clientHeight的用法):

 var height = document.getElementsByClassName('foo')[0].clientHeight; document.getElementById('heightOfFoo').innerHTML = height; 
 <div class="foo">bar</div> <div id="heightOfFoo"></div> 

如果您使用的是jQuery,则可以使用$('。target-class')。height()来获取高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM