简体   繁体   English

使用JQuery获取DIV及其内容的高度

[英]Get height of DIV and its content with JQuery

I have a div which contains several elements: 我有一个div,其中包含几个元素:

<div class="menuItem designMenu" id="dMenu">
  <ul class="menuList menu">
    <li class="menuHeader">Design</li>
    <li class="projectHeader">Mother</li>
    <li class="projectBody">Some text here</li>
    <li class="more">More</li>
  </ul>
</div>

I need to get the height of the dMenu items that I can animate it upwards, including all the content inside. 我需要获得dMenu项的高度,可以向上对其进行动画处理,包括内部的所有内容。 My Javascript currently: 我的Javascript当前:

var designHeight = $("#dMenu").height();

Returns nothing. 不返回任何内容。

I've tried offsetHeight , scrollHeight , and everything else Google turns up. 我试过了offsetHeightscrollHeight ,以及Google出现的所有其他功能。 I'm running the jQuery at the end of the body, inside a document ready function. 我正在正文准备好的函数内部运行jQuery。

The reason to get the height to animate, instead of doing it manually, is that a: I'd like to learn how and b: I don't yet know how many items will be in the div. 使高度进行动画处理而不是手动进行动画处理的原因是:a:我想学习如何; b:我还不知道div中有多少个项目。

Are the li s within the ul floated? ulli是否浮动? If so, and the parent does not have overflow: hidden the parent collapses and therefor has no height. 如果是这样,并且父级没有overflow: hidden则父级折叠并且因此没有高度。 Try (if possible) adding overflow: hidden to the parent element, or hard code a height in your CSS. 尝试(如果可能)添加overflow: hidden到父元素,或在CSS中硬编码一个高度。

I'm using scrollHeight to get at what I want. 我正在使用scrollHeight达到我想要的。 I also, embarrassingly, noticed as I was fiddling with things I was loading my JS before loading JQuery. 令人尴尬的是,我还发现在加载JQuery之前,我对加载JS的事情很不满意。 So ashamed I didn't spot that. 所以感到羞耻,我没发现。

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

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