Fork me on GitHub

sizzle分析记录:getAttribute和getAttributeNode

部分IE游览器下无法通过getAttribute取值?

<form name="aaron">  
  <input type="text" name="aaron"/>  
</form>
alert(form.getAttribute('name')); IE6、7中错误
alert(form.getAttributeNode('name').nodeValue);

看看jQ的解决方案

能力判断

support.attributes = assert(function( div ) {
    div.className = "i"; //设置一个属性
    return !div.getAttribute("className");
});

处理

Sizzle.attr = function( elem, name ) {

    return val !== undefined ?
        val :
        support.attributes || !documentIsHTML ?
            elem.getAttribute( name ) :
            (val = elem.getAttributeNode(name)) && val.specified ?
                val.value :
                null;
};

使用getAttributeNode得到属性节点,再通过nodeValue得到该属性节点的值

getAttributeNode的效率比getAttribute要低一些。如果要获取元素的id,node.id是最快的方法,node.getAttribute('id')其次,getAttributeNode('id').nodeValue最慢。

posted on 2014-07-11 09:57  【艾伦】  阅读(2689)  评论(0编辑  收藏  举报