如何检查浏览器是否支持 getAttributeNode、setAttributeNode 和 createAttribute 函数?
我需要通过JavaScript检测IE6和IE5.5之间的限制,而无需导航器用户代理(使用IEtester或IE控制台模拟器)。
为什么?检查 Modernizr 浏览器支持谢谢!
谢谢奥里奥尔!但更具体地说,我需要这样的东西:
var support = true;
if(typeof(document.getElementsByClassName) === 'undefined'){
support = false;
}
if(support){
// IE > 8
}else{
// IE <= 8
}
但不是 IE 8,而是 IE 5.5。 使用 getAttributeNode、setAttributeNode 和 createAttribute 代替 document.getElementsByClassName
找到了!!!使用来自 http://diveintohtml5.info/detect.html 的 Oriol 答案和视频检测方法
function supports() {
var Element = document.createElement('div'),
Q1 = !!Element.getAttributeNode,
Q2 = !!Element.setAttributeNode,
Q3 = !!document.createAttribute;
return Q1 && Q2 && Q3;
}
if(supports()){
// IE > 5.5
}else{
// IE <= 5.5
}
最佳答案
您可以使用in
运算符来检查对象是否具有某些属性:
'getAttributeNode' in myElement
&& 'setAttributeNode' in myElement
&& 'createAttribute' in document
myElement
应该是对某个元素的引用,例如document.documentElement
.
正确的方法是检查 Element.prototype
和 Document.prototype
,但旧版 IE 不会公开它们。因此,您应该检查某些实例,并假设它也适用于其他实例。
请注意,上面的代码仅测试该属性是否存在。如果你想更安全,你还可以使用 typeof
来检查它们是否是函数。
关于JavaScript - 测试浏览器是否支持 getAttributeNode、setAttributeNode 和 createAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289647/