Element node understanding in the DOM

Source: Internet
Author: User

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
  1. <HEAD>
  2. <TITLE> Empty Valley leisurely </TITLE>
  3. </HEAD>
  4. <BODY>
  5. <table>
  6. <tr>
  7. <TD id= "John" name= "MyName" >John</td>
  8. <td>Doe</td>
  9. <TD id= "Jack" >Jack</td>
  10. </tr>
  11. </table>
  12. <script>
  13. var d = document.getElementById ("John");
  14. alert (d.nodetype)
  15. alert (d.nodename)
  16. alert (d.nodevalue)
  17. </script>
  18. </BODY>
  19. </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
  1. <HEAD>
  2. <TITLE> Empty Valley leisurely </TITLE>
  3. </HEAD>
  4. <BODY>
  5. <table>
  6. <tr>
  7. <TD id= "John" name= "MyName" >John</td>
  8. <td>Doe</td>
  9. <TD id= "Jack" >Jack</td>
  10. </tr>
  11. </table>
  12.   
  13. <script>
  14. var d = document.getElementById ("John"). GetAttributeNode ("name");
  15. alert (d.nodetype)
  16. alert (d.nodename)
  17. alert (d.nodevalue)
  18. </script>
  19. </BODY>
  20. </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
  1. <HEAD>
  2. <TITLE> New Document </TITLE>
  3. </HEAD>
  4. <BODY>
  5. <table>
  6. <tr>
  7. <TD id= "John" name= "MyName" >John</td>
  8. <td>Doe</td>
  9. <TD id= "Jack" >Jack</td>
  10. </tr>
  11. </table>
  12. <script>
  13. var d = document.getelementsbytagname ("TD") [0].firstchild
  14. alert (d.nodetype)
  15. alert (d.nodename)
  16. alert (d.nodevalue)
  17. </script>
  18. </BODY>
  19. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.