In-depth study of Html DOM tree operations

Source: Internet
Author: User

Whether you understand the concept of the html dom tree. Here we will share with you that DOM is a Document Object Model and a set of API Interfaces Based on browser programming, with the W3C recommendation standard, every browser has some minor differences. Javascript alone must be combined with DOM to do DHTML programming, so as to make beautiful results and apply them to the WEB. Therefore, you must have a certain understanding of DOM to apply Javascript to the WEB or your RIA application, because DHTML is essentially an operation on the DOM tree.

DOM tree

The root of the DOM tree is the document root-document. since DOM is a tree structure, they naturally have the following relationships:

◆ Root node (document)

◆ ParentNode)

◆ Child node (childNodes)

Sibling Node

(Sibling)
Example:

Assume that the HTML of the webpage is as follows:

 
 
  1. <title>never-online'swebsite</title> 
  2. <body> 
  3. <div>tutorialofDHTMLandjavascriptprogramming</div> 
  4. </body> 
  5.  

Draw the DOM tree of the HTML document structure by referring to the concept of the tree:

Html
Body head
Divt itle
Text

As shown in the figure above, html has two subnodes, and html is the parent node of these two subnodes. The head has a node title, a text node under the title, and a node div under the doby, div has a text node.

Operate the DOM tree

The beginning has already said that the essence of DHTML is to operate the DOM tree. How to operate it? Suppose I want to change the text of the div node in the above HTML document, how can I do this?

Sample Code:

 
 
  1. <Html>
  2. <Head>
  3. <Title> never-online 'swebsite </title>
  4. <Script>
  5. FunctionchangedivText (strText ){
  6. VarnodeRoot = document; // This is the root node.
  7. VarnodeHTML = nodeRoot. childNodes [0]; // This is an html node.
  8. VarnodeBody = nodeHTML. childNodes [1]; // body Node
  9. VarnodeDiv = nodeBody. childNodes [0]; // DIV Node
  10. VarnodeText = nodeDiv. childNodes [0]; // text node'
  11. NodeText. data = strText; // The text node has the data attribute,
  12. Therefore, we can change this attribute to successfully operate a node in the DOM tree.
  13. }
  14. </Script>
  15. </Head>
  16. <Body>
  17. <Div> tutorialofDHTMLandjavascriptprogramming </div>
  18. <Inputonclickinputonclick = "changedivText ('change? ')"
  19. Type = "button" value = "change"/>
  20. </Body>
  21. </Html>
  22.  

As shown in the preceding example, we can use this method to operate any node on the DOM tree.

Note:

1. Except for cross-origin, cross-origin is usually performed on frame. Simply put, two frames do not belong to the same domain name.

2. in order to demonstrate the above operations, the method used is to traverse from the root node to the text node. There are more concise methods on the DOM method, and more examples will be provided later.

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.