当窗口大小调整时,DIV offsetWidth / Width / innerWidth在IE8中返回零

时间:2014-01-06 10:48:50

标签: javascript jquery html css internet-explorer

我的页面中有很多元素。请考虑下面的DIV

    <div id="root">    
        ..................
    <div id="child1">
        .............
    </div>
    <div id="child2">
        ...............
    </div>
</div>

root div(#root)在加载时基于内容有一些宽度。

当窗口重新调整大小时调用repaint()函数,该函数存在于窗口调整大小函数

$(window).resize( function () {
    repaint();
});
function repaint(){
    //width returns "0" in IE8 browser and other browsers working fine
    var width=$("#root").width() || $("#root").innerWidth() -> returns zero value in IE8
}

它在IE8浏览器中返回宽度为“0”,但在IE9 /其他现代浏览器中返回一些宽度(960px)。

我不知道这个问题的确切原因是什么。

为什么在调整窗口大小时返回零宽度。

谢谢,

希瓦

1 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/5LFXK/

这适用于IE8。查看控制台(布局已设置完毕)。

$(window).resize( function () {
  repaint();
});
function repaint(){
//width returns "0" in IE8 browser and other browsers working fine
var width=$("#root").width();
console.log(width);

}