gpt4 book ai didi

javascript - Internet Explorer 9 对象检测

转载 作者:行者123 更新时间:2023-11-30 08:11:54 26 4
gpt4 key购买 nike

我正在寻找可以识别 IE9 的对象检测能力检查。你能帮帮我吗?

最佳答案

查看 this snippet通过 James Padolsey :

// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
// And to detect the version:
// ie === 6 // IE6
// ie > 7 // IE8, IE9 ...
// ie < 9 // Anything less than IE9
// ----------------------------------------------------------

// UPDATE: Now using Live NodeList idea from @jdalton

var ie = (function(){

var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');

while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);

return v > 4 ? v : undef;

}());

之后,你可以像这样使用它:

if (ie == 9) {
// It’s IE9!
// Insert your code here
}

这里的好处是它不会嗅探 UA 字符串(它本身是不可靠的)——相反,它使用条件注释,这在 IE 中可靠地工作。

这可用于检测 IE5—9。

关于javascript - Internet Explorer 9 对象检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9094527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com