测试浏览器是否支持getAttributeNode, setAttributeNode和createAttribute

JavaScript - Test if getAttributeNode, setAttributeNode and createAttribute are supported by browser

本文关键字:setAttributeNode createAttribute getAttributeNode 浏览器 是否 支持 测试      更新时间:2023-09-26

如何检查浏览器是否支持getAttributeNode, setAttributeNode和createAttribute函数?

我需要通过JavaScript检测IE6和IE5.5之间的限制没有导航用户代理(使用IEtester或IE控制台模拟器)。

为什么?检查Modernizr浏览器支持

谢谢!


谢谢Oriol !但是更具体地说,我需要这样的东西:

var support = true;
if(typeof(document.getElementsByClassName) === 'undefined'){
    support = false;
    }
if(support){
    // IE > 8
    }else{
    // IE <= 8  
        }

但是ie5.5取代了ie8。使用getAttributeNode, setAttributeNode和createAttribute代替document.getElementsByClassName


发现! !使用Oriol答案和视频检测方法从http://diveintohtml5.info/detect.html

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.prototypeDocument.prototype,但旧的IE不暴露它们。因此,您应该检查某些实例,并假设它也适用于其他实例。

注意上面的代码只测试属性是否存在。如果你想更安全,你也可以使用typeof来检查它们是否是函数。

相关文章:
  • 没有找到相关文章