zoukankan      html  css  js  c++  java
  • sizzle分析记录:getAttribute和getAttributeNode

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

  • 相关阅读:
    用struct定义函数
    三、OCTAVE画图
    二、OCTAVE 移动数据
    SQL复习
    Flink处理迟到的数据
    LeetCode题目学习
    CentOS7安装pycharm
    IntelliJ IDEA 刷题利器 LeetCode 插件
    Redis命令学习
    项目杂记
  • 原文地址:https://www.cnblogs.com/aaronjs/p/3837082.html
Copyright © 2011-2022 走看看