码迷,mamicode.com
首页 > 其他好文 > 详细

属性的获取

时间:2014-11-21 16:02:31      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   on   div   log   

jQuery.attr , Sizzle.attr

Sizzle.attr = function(elem, name) {
    // Set document vars if needed
    if ((elem.ownerDocument || elem) !== document) {
        setDocument(elem);
    }

    var fn = Expr.attrHandle[name.toLowerCase()],
        // Don‘t get fooled by Object.prototype properties (jQuery #13807)
        val = fn && hasOwn.call(Expr.attrHandle, name.toLowerCase()) ?
            fn(elem, name, !documentIsHTML) :
            undefined;

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

在IE6 7中getAttribute返回属性而不是属性

// Support: IE<8
// Verify that getAttribute really returns attributes and not properties (excepting IE8 booleans)
support.attributes = assert(function(div) {
    div.className = "i";
    return !div.getAttribute("className");
});

所以jquery采用getAttributeNode方法处理

获取属性值 - getAttribute()

getAttribute("") 方法返回属性的值。


获取属性值 - getAttributeNode()

getAttributeNode("") 方法返回属性节点,getAttributeNode(‘‘).value取得节点值。
 
 
<li class="aa"></li>
li.getAttributeNode(‘class‘)  // class="aa"
li.getAttribute(‘class‘) //aa
 
 
特别指出加强specified的判断是因为,在IE6 7会返回所有的节点,只需要返回有用的特性节点

 

属性的获取

标签:style   blog   io   ar   color   sp   on   div   log   

原文地址:http://www.cnblogs.com/aaronjs/p/4112769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!