Html 获取动态改变高度 div 的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4371171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:37:07  来源:igfitidea点击:

Get height of dynamically changed height div

csshtmlheight

提问by Roman

How can I determine (using jQuery?) the height of a div? Its' height is not defined in CSS - so it's fluid and based on the contents.

如何确定(使用 jQuery?)div 的高度?它的高度不是在 CSS 中定义的 - 所以它是流动的并且基于内容。

I've tried $('#div').height() - which returns 0.

我试过 $('#div').height() - 它返回 0。

Ideas?

想法?

EDIT: (the code)

编辑:(代码)

$(document).ready(function () {
    PositionBottomPicture();
});

function PositionBottomPicture() {
    var parentOffset = $('#left_pane').offset();
    var parentsHeight = $('#left_pane').height();
    var childsTopPostion = (parentOffset.top + parentsHeight);


    $('#bottom_pic').offset({ top: childsTopPostion, left: parentOffset.left });
}

CSS:

#left_pane 
{
    float: left;
    margin-left: 27px;
    position: relative;
}

where 'left_pane' and 'bottom_pic' are divs.

其中 'left_pane' 和 'bottom_pic' 是 div。

Thanks!

谢谢!

回答by RobertPitt

The problem is you're probably not waiting for the div to load into the DOM.

问题是您可能没有等待 div 加载到 DOM 中。

Try something along the lines of:

尝试一些类似的东西:

$(document).ready(function(){
        var h = $("#div").height();
        var w = $("#div").width();
;})

Using the Document Ready tool provided by jQuery will wait till the element has been processed.

使用 jQuery 提供的 Document Ready 工具将等待元素处理完毕。

回答by kobe

Check this jsfiddle example , it retuns width and height

检查这个 jsfiddle 示例,它重新调整宽度和高度

http://jsfiddle.net/b2DNP/

http://jsfiddle.net/b2DNP/

回答by davidbuttar

If 'left_pane' only contains absolutely positioned elements it's height will be zero.

如果 'left_pane' 只包含绝对定位的元素,它的高度将为零。