Javascript 在IE中将div设置为自动后获取div的高度
我正在编写一些JavaScript来更改某些内容的大小。为此,我需要知道我的内容中div的大小。如果我有以下html:Javascript 在IE中将div设置为自动后获取div的高度,javascript,jquery,css,Javascript,Jquery,Css,我正在编写一些JavaScript来更改某些内容的大小。为此,我需要知道我的内容中div的大小。如果我有以下html: <div id="wrapper"> ... other stuff ... <div id="inner" style="height:400px">Some text in here</div> ... other stuff ... </div> 在FireFox和Chrome中,高度变量随着内部div的
<div id="wrapper">
... other stuff ...
<div id="inner" style="height:400px">Some text in here</div>
... other stuff ...
</div>
在FireFox和Chrome中,高度变量随着内部div的扩展而增加,以适应所有文本。在IE中,这个保持不变。我想它不会马上重新绘制分区。有人知道如何在IE中获得新的正确高度吗
干杯如果问题是元素没有立即重新绘制,则需要异步测量高度-设置0毫秒超时以测量高度并继续执行。请尝试以下方法:
$(function() {
$('#inner').height('auto');
var height = $("#wrapper").height();
});
将其包装在
$(function(){})中代码>使其在运行脚本之前等待DOM渲染。在渲染后立即将任何需要访问DOM的内容放入该函数中可能是一个好主意。您是运行怪癖模式还是标准模式?
$(function() {
$('#inner').height('auto');
var height = $("#wrapper").height();
});