0

我用jquery在#main上附加了很多DIV,而#main的高度是auto,那么我想得到#main的总高度,但是高度总是0,那么如何才能得到#main的总高度通过jquery插入许多DIV后?可以为我创建一个 DEMO 吗?非常感谢先进!

4

2 回答 2

0

检查此演示http://jsfiddle.net/yeyene/HJ4Gc/1/

$('#add').on('click',function(){
    $('#main').append('<div>I used jquery to appended many DIVs to #main, and the height of #main is auto, then I want to get the total height of #main, but the height is always 0, so how can I get the total height of #main after insert many DIV by jquery? Is there possible to create a DEMO for me? Thanks so much in advanced!<br /></div>');
});
$('#getHeight').on('click',function(){
    alert($('#main').height());
});
于 2013-06-24T03:56:01.260 回答
0

选项 1)如果您知道 div 的高度,请不要费心计算 #main 的高度,只需添加 div 的高度 + 它们的顶部和底部填充 + 它们的顶部和底部边距。

选项 2)给你的#main 一个背景颜色,并确保它实际上扩展到插入的 div 的大小。如果不是,那么您可以轻松检索高度,您只需要弄清楚它为什么没有扩展。也许您在没有清除任何内容或使用溢出属性的情况下浮动了内部 div?

选项 3)您是否在完全创建 div 之前检查高度?尝试延迟检索高度以查看它是否在 div 加载之前进行检查。

于 2013-06-24T03:47:50.163 回答