【问题标题】:$(window).width() not working in IE9$(window).width() 在 IE9 中不起作用
【发布时间】:2012-02-09 17:03:53
【问题描述】:

我正在做类似以下的事情:

// get the screen height and width 
var maskHeight = $(document).height(); 
var maskWidth = $(window).width();

// calculate the values for center alignment
var dialogLeft = (maskWidth/2) - ($('#dialog-box').width()/2);

但看起来它在 IE9 中不起作用。

【问题讨论】:

  • 到底做错了什么?
  • 他不知何故向右移动
  • @Saurabh 您应该缓存您的 $('#dialog-box') 参考:var dialog = $('#dialog-box')[0];(在页面加载时),然后在需要时使用 $(dialog)...
  • @Saurabh $(window).width() 应该可以正常工作。问题出在其他地方...您能提供问题的屏幕截图吗?

标签: javascript jquery internet-explorer-9 cross-browser


【解决方案1】:

试试这个:

var maskWidth = window.innerWidth;
var maskHeight = window.innerHeight;

或者在 IE 6+ 的标准兼容模式下:

var maskWidth = document.documentElement.clientWidth;
var maskHeight = document.documentElement.clientHeight;

【讨论】:

    【解决方案2】:

    用途:

    $(window).innerHeight();
    $(window).innerWidth();
    

    【讨论】:

    • 不适用于窗口对象...(docs 明确说明)
    猜你喜欢
    • 2013-08-12
    • 2011-01-03
    • 1970-01-01
    • 2012-03-05
    • 2018-04-29
    • 2012-02-09
    • 2012-08-02
    • 2014-05-04
    • 2011-07-29
    相关资源
    最近更新 更多