首页 > 其他 > 详细

sizzle分析记录:getAttribute和getAttributeNode

时间:2014-07-16 20:06:29      阅读:370      评论:0      收藏:0      [点我收藏+]

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

sizzle分析记录:getAttribute和getAttributeNode,布布扣,bubuko.com

sizzle分析记录:getAttribute和getAttributeNode

原文:http://www.cnblogs.com/aaronjs/p/3837082.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!