使用js获取带有嵌套绝对div的div的高度

发布于 2024-11-30 17:10:46 字数 618 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2024-12-07 17:10:46

我认为你搞错了。当您像这样将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文