如何获得元素的渲染高度?
问题描述:
如何获得元素的渲染高度?
How do you get the rendered height of an element?
假设你有一个< div>
里面有一些内容的元素。里面的这个内容将拉伸< div>
的高度。如果没有明确设置高度,如何获得渲染高度。显然,我试过:
Let's say you have a <div>
element with some content inside. This content inside is going to stretch the height of the <div>
. How do you get the "rendered" height when you haven't explicitly set the height. Obviously, I tried:
var h = document.getElementById('someDiv').style.height;
这样做有诀窍吗?如果有帮助,我正在使用jQuery。
Is there a trick for doing this? I am using jQuery if that helps.
答
它应该只是
$('#someDiv').height();
。这将检索包装集中第一个项目的高度作为数字。
with jQuery. This retrieves the height of the first item in the wrapped set as a number.
尝试使用
.style.height
仅在您首先设置属性时才有效。不太有用!
only works if you have set the property in the first place. Not very useful!