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

     
  • 相关阅读:
    Traits——信息输入界面
    Traits——安装/第一个界面
    opencv——如何安装opencv—python
    python——如何将列表中的元素全部取出来变成列表
    Pandas——循环路径下的文件将所有的txt文件进行合并
    股票交易
    良知?
    同源策略
    同步 异步 阻塞 非阻塞
    线程安全
  • 原文地址:https://www.cnblogs.com/aaronjs/p/4112769.html
Copyright © 2011-2022 走看看