zoukankan      html  css  js  c++  java
  • js中的attribute详解

    Attribute是属性的意思,文章仅对部分兼容IE和FF的Attribute相关的介绍。

    attributes:获取一个属性作为对象

    getAttribute:获取某一个属性的值
    setAttribute:建立一个属性,并同时给属性捆绑一个值
    createAttribute:仅建立一个属性
    removeAttribute:删除一个属性

    getAttributeNode:获取一个节点作为对象
    setAttributeNode:建立一个节点
    removeAttributeNode:删除一个节点

    attributes可以获取一个对象中的一个属性,并且作为对象来调用,注意在这里要使用“[]”,IE在这里可以使用“()”,考虑到兼容性的问题,要使用“[]”。关于attributes属性的使用方式上,IE和FF有巨大的分歧,在此不多介绍。

    attributes的使用方法:(IE和FF通用)

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.getElementById("sss").attributes["value"];
    document.write(d.name);
    document.write(d.value);
    //显示value aaa
    </script>

    getAttribute,setAttribute,createAttribute,removeAttribute四兄弟的概念比较容易理解,使用方法也比较简单,唯一需要注意这几点:

    1、createAttribute在使用的时候不需要基于对象的,document.createAttribute()就可以。

    2、setAttribute,createAttribute在使用的时候如果是使用的时候不要使用name,type,value等单词,IE都FF的反应都奇怪的难以理解。

    3、createAttribute在使用的时候如果只定义了名字,没有d.nodeValue = "hello";语句定义值,FF会认为是一个空字符串,IE认为是undefined,注意到这点就可以了。

    getAttribute的使用方法:

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.getElementById("sss").getAttribute("value");
    document.write(d);
    //显示 aaa
    </script>

    setAttribute的使用方法:(你会发现多了一个名为good的属性hello)

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.getElementById("sss").setAttribute("good","hello");
    alert(document.getElementById("t").innerHTML)
    </script>

    createAttribute的使用方法:(多了一个名为good的空属性)

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.createAttribute("good");
    document.getElementById("sss").setAttributeNode(d);
    alert(document.getElementById("t").innerHTML)
    </script>

    removeAttribute的使用方法:(少了一个)

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.getElementById("sss").removeAttribute("value");
    alert(document.getElementById("t").innerHTML)
    </script>

    getAttributeNode,setAttributeNode,removeAttributeNode三个方法的特点是都直接操作一个 node(节点),removeAttributeNode在一开始的时候总会用错,但是充分理解了node的含义的时候,就能够应用自如了。

    getAttributeNode的使用方法:

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.getElementById("sss").getAttributeNode("value");
    document.write(d.name);
    document.write(d.value);
    //显示 value aaa
    </script>

    setAttributeNode的使用方法:

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.createAttribute("good");
    document.getElementById("sss").setAttributeNode(d);
    alert(document.getElementById("t").innerHTML);
    </script>

    removeAttributeNode的使用方法:

    <body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
    </body>
    <script>
    var d = document.getElementById("sss").getAttributeNode("value")
    document.getElementById("sss").removeAttributeNode(d);
    alert(document.getElementById("t").innerHTML);
    </script>
    更多的关于attributes属必性问题,可在w3school中查询!
     
    转载:http://blog.csdn.net/firesnake666/article/details/6031131
  • 相关阅读:
    异常-ERROR yarn.ApplicationMaster: User class threw exception: java.sql.SQLException: Communications link failure
    异常-Data truncation: Truncated incorrect DOUBLE value: '-9370.3530-'
    算法中的时间复杂度分析
    MySQL开启binlog无法启动ct 10 21:27:31 postfix/pickup[4801]: warning: 6BD991A0039: message has been queue
    【异常】ERROR main:com.cloudera.enterprise.dbutil.SqlFileRunner: Exception while executing ddl scripts. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'ROLES' already exists
    CDH5.16.1的agent启动报错:ERROR Error, CM server guid updated, expected d9bcadb4-f983-41b8-a667-66760f47bc91, received a67f5efa-8473-4f6a-94d6-231d1f432ef0
    MySQL无法启动:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
    [Ubuntu] Ubuntu13.04, the desktop freezed after login
    [ubuntu] ubuntu13.04安装rabbitcvs管理svn
    [javascript] ajaxfileupload.js 跨域上传文件
  • 原文地址:https://www.cnblogs.com/fulai/p/3389061.html
Copyright © 2011-2022 走看看