Node information
Each node has properties that contain some information about the node. These properties are:
- NodeName (node name)
- NodeValue (node value)
- NodeType (node type)
NodeType
The NodeType property returns the type of the node.
The most important node types are:
element Type |
node Type |
Elements |
1 |
Property |
2 |
Text |
3 |
Comments |
8 |
Document |
9 |
in the actual application, is often used is the element node, attribute node and text node, below we through the small section code to explain
1: Element node
ELEMENT Node code
- <HEAD>
- <TITLE> Empty Valley leisurely </TITLE>
- </HEAD>
- <BODY>
- <table>
- <tr>
- <TD id= "John" name= "MyName" >John</td>
- <td>Doe</td>
- <TD id= "Jack" >Jack</td>
- </tr>
- </table>
- <script>
- var d = document.getElementById ("John");
- alert (d.nodetype)
- alert (d.nodename)
- alert (d.nodevalue)
- </script>
- </BODY>
- </HTML>
<HEAD> <TITLE> Empty Valley leisurely </TITLE> </HEAD> <BODY> <table><tr><td id= "John" Name= "myname" >john</td><td>doe</td><td id= "Jack" >Jack</td></tr> </table> <script> var d = document.getElementById ("John"), alert (d.nodetype) alert (d.nodename) Alert (d.nodevalue) </script> </BODY></HTML>
Analyze the result of the run with the values of the three properties:
Nodetype:element_node
NodeType Value: 1
NodeName: Element tag name//here is TD
Nodevalue:null
2: Attribute node
Attribute Node code
- <HEAD>
- <TITLE> Empty Valley leisurely </TITLE>
- </HEAD>
- <BODY>
- <table>
- <tr>
- <TD id= "John" name= "MyName" >John</td>
- <td>Doe</td>
- <TD id= "Jack" >Jack</td>
- </tr>
- </table>
-
- <script>
- var d = document.getElementById ("John"). GetAttributeNode ("name");
- alert (d.nodetype)
- alert (d.nodename)
- alert (d.nodevalue)
- </script>
- </BODY>
- </HTML>
<HEAD> <TITLE> Empty Valley leisurely </TITLE> </HEAD> <BODY> <table><tr><td id= "John" Name= "myname" >john</td><td>doe</td><td id= "Jack" >Jack</td></tr> </table> <script> var d = document.getElementById ("John"). GetAttributeNode ("name"); Alert ( D.nodetype) alert (d.nodename) alert (d.nodevalue) </script> </BODY></HTML>
Analyze the result of the run with the values of the three properties:
Nodetype:attribute_node
NodeType Value: 2
NodeName: Attribute name//name
NodeValue: Attribute value//myname
3: Text node
Text Node code
- <HEAD>
- <TITLE> New Document </TITLE>
- </HEAD>
- <BODY>
- <table>
- <tr>
- <TD id= "John" name= "MyName" >John</td>
- <td>Doe</td>
- <TD id= "Jack" >Jack</td>
- </tr>
- </table>
- <script>
- var d = document.getelementsbytagname ("TD") [0].firstchild
- alert (d.nodetype)
- alert (d.nodename)
- alert (d.nodevalue)
- </script>
- </BODY>
- </HTML>
<HEAD> <TITLE> New Document </TITLE> </HEAD> <BODY> <table><tr> <TD id= "John" Name= "myname" >john</td><td>doe</td><td id= "Jack" >jack</td></ tr> </table> <script> var d = document.getelementsbytagname ("TD") [0].firstchildalert ( D.nodetype) alert (d.nodename) alert (d.nodevalue) </script> </BODY></HTML>
Analyze the result of the run with the values of the three properties:
Nodetype:text_node
NodeType Value: 3
NodeName: #text
NodeValue: Text content//John
Element node understanding in the DOM