简体   繁体   English

如何在Internet Explorer 8中获取innerWidth

[英]How Do I Get innerWidth in Internet explorer 8

In all recent browser: 在最近的浏览器中:

 window.innerWidth // 1920

In Internet explorer 8 在Internet Explorer 8中

 window.innerWidth // undefined

What is the best way to get this value in IE8? 在IE8中获得此值的最佳方法是什么?

The innerWidth is supported by IE9 not IE8, you can do this insteaad: IE9不支持IE8支持innerWidth ,你可以这样做:

var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;

The above line will get you the width from IE as well as other standard-compliant browsers. 以上行将为您提供IE以及其他符合标准的浏览器的宽度。


If you use jQuery, $(window).innerWidth() will give you desired result in all browsers too. 如果你使用jQuery, $(window).innerWidth()也会在所有浏览器中提供你想要的结果。

For getting this, I still use: 为了得到这个,我仍然使用:

window.innerWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
window.innerHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

But to imitate the real dom-getter take a look at this: https://stackoverflow.com/a/18136089/1250044 但要模仿真正的dom-getter看看这个: https//stackoverflow.com/a/18136089/1250044

用于ie8使用

document.documentElement.clientWidth

I know that the innerWidth might not be quite the same, but getBoundingClientRect could also be used in a similar capacity: 我知道innerWidth可能不完全相同,但getBoundingClientRect也可以以类似的容量使用:

elem = document.documentElement.getBoundingClientRect();
width = elem.getBoundingClientRect().right - elem.getBoundingClientRect().left;

You can get this information from IE 8 with 您可以从IE 8获取此信息

document.documentElement.clientWidth

Be advised that this value will not be exactly the same that IE 9 returns for window.innerWidth . 请注意,此值与IE 9为window.innerWidth返回的值不完全相同。

Without jQuery you can try this to get height and width 没有jQuery,你可以尝试这个来获得heightwidth

var myWidth = 0, myHeight = 0;
if (typeof (window.innerWidth) == 'number') { //Chrome
     myWidth = window.innerWidth; 
     myHeight = window.innerHeight;
} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
     myWidth = document.documentElement.clientWidth; 
     myHeight = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { //IE9
     myWidth = document.body.clientWidth; 
     myHeight = document.body.clientHeight;
}
document.documentElement.clientWidth;

这用于ie8以获取客户端宽度

Please note: .innerWidth method is not applicable to window and document objects; 请注意:.innerWidth方法不适用于窗口和文档对象; for these, use .width() instead. 对于这些,请改用.width()。

jquery api documentation for .innerwidth() .innerwidth()的jquery api文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何获得提示(例如cluetip)以在Internet Explorer中工作? - how do i get a tooltip such as cluetip, to work in Internet Explorer? 如何让JavaScript生成的图像映射与Internet Explorer一起使用? - How do I get JavaScript-generated image maps to work with Internet Explorer? 如何从Internet Explorer获取在JavaScript中打开窗口的iframe? - How do I get the iframe that opened a window in JavaScript from Internet Explorer? 我需要什么才能在Internet Explorer中使用JavaScript? - What do i need to get javascript working in Internet Explorer? 如何从Internet Explorer中的剪贴板获取base64编码图像? - How do i get base64 encoded image from clipboard in internet explorer? 你如何获得window.open在Internet Explorer 7中工作? - How do you get window.open to work in internet explorer 7? window.innerWidth不适用于Internet Explorer Windows Mobile - window.innerWidth won't work with internet explorer windows mobile 如何在 Windows Phone 7 上调试 Internet Explorer? - How do I debug Internet Explorer on Windows Phone 7? 如何更改“睡眠”功能以在Internet Explorer中工作? - How do I change the “sleep” function to work in Internet Explorer? 如何在Internet Explorer中禁用复选框 - How do I keep a Checkbox disabled in Internet Explorer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM