【问题标题】:DIV offsetWidth/Width/innerWidth returns zero in IE8 when window resizing调整窗口大小时,DIV offsetWidth/Width/innerWidth 在 IE8 中返回零
【发布时间】:2014-01-23 18:12:43
【问题描述】:

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

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

根 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)。

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

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

谢谢,

湿婆

【问题讨论】:

  • 可能是浏览器渲染问题,尝试超时测试
  • 您在此处粘贴的代码中没有关闭#root div。
  • 我认为,这是由于元素显示/位置 CSS 属性造成的。
  • @Ishan 我没有用于根元素的 css。你能解释一下吗?哪个css属性会抛出这个错误

标签: javascript jquery html css internet-explorer


【解决方案1】:

http://jsfiddle.net/5LFXK/

这适用于 IE8。看控制台(布局是f***ed up)。

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

}

【讨论】:

  • 这是由于元素显示/位置 CSS 属性造成的。
  • 你在这里看到了一些 CSS 吗?
  • 你是如何在 IE8 中运行 jsfiddle 的?我无法在 IE8 浏览器中运行它
  • 试试这个:jsfiddle.net/5LFXK/2(在 IE 8 JsFiddle 无法正常工作,这里我只更新 @enguerranws fiddle 和一些 alert(),所以如果你改变窗口大小小提琴会提醒你这个事件.)
  • 您没有收到警报?还是警报 0 ?如果没有提示,可能是你的 IE 没有激活 JS。
猜你喜欢
  • 2018-02-20
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多