JavaScript DOM document traversal (1)

Source: Internet
Author: User

This document introduces how to use JavaScript DOM to modify document tree and how to use JavaScript DOM to create and clone elements, let's start with the most basic elements of the HTML page:

 
 
  1. <!--
  2.     
  3.         <title>DOM Examlie</title> 
  4.     
  5.     <body> 
  6.        <p>Hello World !</p> 
  7. </body> 

When accessing the

 
 
  1. Var ohtml#document.doc umentElement; // you can directly access the
  2. Alert ("node name:" + oHtml. nodeName); // node name
  3. Alert ("Node Type:" + oHtml. nodeType); // The node type is 1

Obtain the

 
 
  1. Var oHead = oHtml. firstChild; // HEAD node
  2. Var oBody = oHtml. lastChild; // BODY Node

You can also use the childNodes attribute to obtain the

 
 
  1. Var oHead = oHtml. childNodes. item (0); // HEAD node
  2. // Var oHead = oHtml. childNodes [0]; // abbreviation, the same result is the HEAD node.
  3. Var oBody = oHtml. childNodes. item (1); // BODY Node
  4. // Var oBody = oHtml. childNodes. item (1); // abbreviation. The same result is the BODY node.

Note: The square brackets mark is actually a simple implementation of NodeList in javascript. In fact, the method for obtaining subnodes from the childNodes list is to use the item () method: document. body, a proprietary attribute in the html dom, which is often used to directly access elements:

 
 
  1. var oBody=document.body;  

Since we all know how to obtain the above node objects, we use the three variables oHtml, oHead, and oBody to determine the relationship between them:

 
 
  1. Alert (oHead. parentNode = oHtml); // The parent node of the HEAD node is the BODY node, and true is returned.
  2. Alert (oBody. parentNode = oHtml); // The parent node of the BODY node is the BODY node, and true is returned.
  3. Alert (oBody. previussibling = oHead); // The Last sibling node of the BODY node is the HEAD node, and true is returned.
  4. Alert (oHead. nextSibling = oBody); // The next sibling node of the HEAD node is the BODY node, and true is returned.
  5. Alert (oHead. ownerDocument = document); // returns the root element (Document) of a node. returns true if the HEAD node points to this document.

Through the above learning, we have learned the most basic way to traverse a node and how to find the brother node and its child node of a node.


Related Article

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.