Comments on the attribute js

Attribute is meant the property, only part of the article is compatible with the Attribute IE and FF related description. attributes: Get a property as an object getAttribute: Gets the value of a certain attribute
object.getAttributes (attribute) getAttribute method does not belong to the document object, the document object can not be acquired only by calling element nodes. E.g. document.getElementsByTagName ( "P") [0].
GetAttributes ( "title")
the setAttribute: establishing a property value and at the same time give a binding property
allows to make changes to attribute nodes, e.g.

var shoop=document.getElementsById("psdf'); shoop.setAttribute("tittle","a lot of goods")

createAttribute: build only one attribute
removeAttribute: delete a property
getAttributeNode: Gets a node as an object
setAttributeNode: the establishment of a node
removeAttributeNode: delete a node
attributes can get a property of an object, and as an object to call attention here to use. " [] "may be used herein IEs in" () ", taking into account the compatibility issues, to use the" []. " About the use of the property attributes, IE and FF have great differences in this little introduction. The use attributes: (IE and FF generic)

<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, four brothers removeAttribute concept easier to understand
, to use relatively simple, the only need to pay attention to these points:
1, when in use does not require createAttribute object-based, document.createAttribute () on it.
2, setAttribute, when createAttribute when in use if it is used not to use name, type, value and other words, IE all FF's strange reactions hard to understand.
3, createAttribute when in use, if only defines the name, no d.nodeValue = "hello"; statement defines value, FF will be considered to be an empty string, IE considered to be undefined, noticed this on it.
4 \ getAttribute use:

<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>

Features getAttributeNode, setAttributeNode, removeAttributeNode three methods is directly operating a node (node), removeAttributeNode at the beginning of time always wrong, but to fully understand the meaning of a node when it can be applied comfortable with. getAttributeNode use:

<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 use:

<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>

Guess you like

Origin www.cnblogs.com/shun1015/p/11515447.html