使用js获取带有嵌套绝对div的div的高度
我有一个 div 作为一个相对定位的容器。 在这个 div 中,我还有 3 个其他 div 被定位为绝对。
<head>
<style>
#container{ position:relative; }
#block1, #block2, #block3 { position:absolute; }
#block2 { top:100px; }
#block3 { top:600px; }
</style>
</head>
<div id="container">
<div id="block1"> some text </div>
<div id="block2"> some text </div>
<div id="block3"> some text </div>
</div>
如何使用 js 获得相对 div 的正确高度。
我尝试了 .clientHeight 和 jquery .height() 但不起作用。
谢谢!
I've got a a div acting as a container which is positioned as relative.
Within this div I have 3 other divs positioned as absolute.
<head>
<style>
#container{ position:relative; }
#block1, #block2, #block3 { position:absolute; }
#block2 { top:100px; }
#block3 { top:600px; }
</style>
</head>
<div id="container">
<div id="block1"> some text </div>
<div id="block2"> some text </div>
<div id="block3"> some text </div>
</div>
How can I get correct height of a relative div with js.
I tried .clientHeight and jquery .height() but won't work.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你搞错了。当您像这样将 div 放置在其他 div 内时,container-div 将几乎没有(或根本没有)高度。这就是绝对定位 div 的原因。它们可以放置在 container-div 的“外部”,并且仅将其用作 x 和 y 偏移量的参考。
I think you got something wrong. When you position divs inside other divs like that, the container-div will have nearly no (or no) height at all. That's cause of the absolute positioned divs. They can be placed "outside" the container-div, and only use it as a reference for x and y offsets.