zoukankan      html  css  js  c++  java
  • 属性的获取

    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会返回所有的节点,只需要返回有用的特性节点

     
  • 相关阅读:
    more命令
    mktemp命令
    有效的括号字符串
    mc命令
    字符串相加
    Vue中虚拟DOM的理解
    chattr命令
    记近一年线上项目经验及架构变更记录
    微博AnalysisQl动态数据视图元数据设计
    搭建prometheus+grafana监控SpringBoot应用入门
  • 原文地址:https://www.cnblogs.com/aaronjs/p/4112769.html
Copyright © 2011-2022 走看看