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:
-
-
- <title>never-online'swebsite</title>
-
- <body>
- <div>tutorialofDHTMLandjavascriptprogramming</div>
- </body>
-
-
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:
- <Html>
- <Head>
- <Title> never-online 'swebsite </title>
- <Script>
- FunctionchangedivText (strText ){
- VarnodeRoot = document; // This is the root node.
- VarnodeHTML = nodeRoot. childNodes [0]; // This is an html node.
- VarnodeBody = nodeHTML. childNodes [1]; // body Node
- VarnodeDiv = nodeBody. childNodes [0]; // DIV Node
- VarnodeText = nodeDiv. childNodes [0]; // text node'
- NodeText. data = strText; // The text node has the data attribute,
- Therefore, we can change this attribute to successfully operate a node in the DOM tree.
- }
- </Script>
- </Head>
- <Body>
- <Div> tutorialofDHTMLandjavascriptprogramming </div>
- <Inputonclickinputonclick = "changedivText ('change? ')"
- Type = "button" value = "change"/>
- </Body>
- </Html>
-
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.