zl程序教程

您现在的位置是:首页 >  其它

当前栏目

sizzle分析记录:getAttribute和getAttributeNode

分析 记录 getAttribute
2023-09-14 08:57:46 时间

部分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最慢。