[英]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,你可以尝试这个来获得
height
和width
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.