Javascript beginners Article 6 Javascript DOM programming page 1

Source: Internet
Author: User


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:

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.