JavaScript - 测试浏览器是否支持getAttributeNode、setAttributeNode和createAttribute

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

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

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

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

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