用jQuery获取绝对DIV的高度 [英] Getting height of an absolute DIV with jQuery
问题描述
我正在尝试使用非预定义高度属性的绝对定位DIV的 height
属性。
I am trying to the the height
attribute of an absolute positioned DIV with a non predefined height attribute.
div
的高度取决于其动态内容。
The height of the div
depends on its dynamic content.
我已经看到 .height()
jQuery的函数没有返回绝对定位的正确高度 div
。
I've seen that .height()
function of jQuery doesn't return the correct height of an absolute positioned div
.
$(this).height() //not working well
有没有办法获得它的高度?
Is there any way to get its height?
谢谢。
推荐答案
你需要在div中包含一些内容或至少定义一些最小高度才能获得css height属性 - 否则未定义。
You need to have some content inside the div or at least define some minimum height in order to get the css height property—which is otherwise undefined.
.height()
获取元素的计算高度,因此需要一些预定义。
The .height()
gets the computed height for the element and thus needs some predefining.
替代方案是 .innerHeight()
和 .outerHeight()
方法。如果您使用默认的内容框样式,它们可能会更好。
Alternatives are the .innerHeight()
and .outerHeight()
methods. They might work better if you are working with the default content-box style.
这篇关于用jQuery获取绝对DIV的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!