Jquery – 获取构造div的高度

我正在构建一个div并将其附加到主div.在这样做的时候,我需要在构建它之前和附加它之前获得div的高度.

例如,

<script>
$(function(){
var text = $("#ObjText").html(); // This text is dynamic

var dv = $("<div id='childDiv'>"+text+"</div>");
alert($(dv).height()); // This is getting '0' - Need to get the height of the div here.

$("#page").append(dv);

//After appending to page able to get the height of the child div
alert($("#childDiv").height());
});
</script>

在我的身体标签,

<body>
<div id="page"></div>
</body>

请帮我解决这个问题.提前致谢.

解决方法

在将其附加到文档之前,我认为您无法获得在运行时创建的任何对象的高度.

jQuery执行并获取文档上所有DOM就绪对象的结果(你的html)因此,在追加你之前你无法获得高度.

如果你仍然想要获得高度,我可以建议你在html上临时放置div,你可以附加你当前的div并检查高度.如果你想要你并将div追加到你想要的地方.

希望以上帮助您找到解决方案……

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...