Learning English:
DOM: Document Object Model. Document Object Model
Bom: browser object model.
Note: It can also be called the window object model. (Window object model .)
API: Application Programming Interface.
Note: Dom can be viewed as an API.
Node: node.
Note: nodes are divided into element nodes, attribute nodes, and text nodes.
Element nodes include attribute nodes and text nodes.
DOM tree:
The following describes how to operate the Dom.
1. Create an element node. Createelement ():
<Script language = "JavaScript">
VaR A = Document. createelement ("p ");
Alert ("Node Type:" + A. nodetype + ", node name:" + A. nodename );
</SCRIPT>
Output; nodetype is 1. A. nodename is P;
So it creates an element node .... You may wonder why node P is not found in the document?
Let's look at the following example:
<Body>
</Body>
<Script language = "JavaScript">
VaR A = Document. createelement ("p ");
Document. Body. appendchild ();
</SCRIPT>
You can use firebug to view the results that we need in the document.
The new element node created by the createelement () method is not automatically added to the document. Since it is not added to the document, it indicates that it is still in a free state. If you want to add it to the document, you can use the appendchild (), insertbefore (), or replaceChild () methods (described later ).
2. Create a text node. Createtextnode ():
VaR B = Document. createtextnode ("My demo ");
Alert ("Node Type:" + B. nodetype + ", node name:" + B. nodename );
Output; nodetype is 3. A. nodename is # text;
So it creates a text node .... You may wonder why this text node is not found in the document? Like createelement (), you must use appendchild () to add it to the document.
Yes, you have a very pair of ideas.
Let's look at the following example:
<Body>
</Body>
<Script language = "JavaScript">
VaR MEs = Document. createtextnode ("Hello World ");
VaR Container = Document. createelement ("p ");
Container. appendchild (MES); // Add the text node to the element node first
Document. Body. appendchild (container); // Add the element node to the document.
</SCRIPT>
3. Copy the node. Clonenode (Boolean): a parameter:
Let's look at an example:
<Body>
</Body>
<Script language = "JavaScript">
VaR MEs = Document. createtextnode ("Hello World ");
VaR Container = Document. createelement ("p ");
Container. appendchild (MES );
Document. Body. appendchild (container );
VaR newpara = container. clonenode (true); // difference between true and false
Document. Body. appendchild (newpara );
VaR newpara = container. clonenode (false); // difference between true and false
Document. Body. appendchild (newpara );
</SCRIPT>
Check the results of firebug: