JavaScript DOM practice: Creating and cloning Elements

Source: Internet
Author: User

DOM allows you to create your own elements and text nodes and add them to the large document tree. Theoretically, you can remove all elements from the tree, create new elements, or restructure the document tree to completely change the page. However, in practice, this feature has some limitations. DOM also allows you to clone existing elements, so you can easily copy a part of the document and distribute copies in the document.

BKJIA recommended reading: The nature and operation methods of JavaScript DOM

CreateElement () and createTextNode ()

CreateElement () and createTextNode () do what they call. The most common JavaScript DOM method practices-they have been used to modify the document tree. The purpose was to add the newly created element to the document tree and make it the last subnode of an element.

 
 
  1. // Create a new li Element
  2. Var newChild = document. createElement ('lil ');
  3. // Create a new a Element
  4. Var newLink = document. createElement ('A ')
  5. // Create a Text node
  6. Var newText = document. createTextNode ('My Wiki ');

NewChild points to the newly created <li> element object, newLink points to the newly created <a> element object, and newText points to the newly created text Node object. These nodes have not been inserted into the document. The most common JavaScript DOM method practice-appendChild () or insertBefore () used in modifying the document tree to attach them to the document tree. For example:

 
 
  1. Var nav = document. getElementById ("nav ");
  2. // Create a new li Element
  3. Var newChild = document. createElement ('lil ');
  4. // Create a new a Element
  5. Var newLink = document. createElement ('A ')
  6. // Create a Text node
  7. Var newText = document. createTextNode ('My Wiki ');
  8. // Add Text to element
  9. NewLink. appendChild (newText );
  10. // Set the href and content for element
  11. NewLink. setAttribute ('href ',"#");
  12. // Add Element a to the new li Element Node
  13. NewChild. appendChild (newLink );
  14. // Add the new li element node to the ul Element Node
  15. Nav. appendChild (newChild );

Add the text node to <a>, and then <a> attach the node that contains the text node to <li>, finally, add <a> and <li> of the text to <ul>. At this time, I have a li subnode in the navigation bar ul.

CreateTextNode () and HTML Object

CreateTextNode () has a problem: it cannot create a symbol similar to & euro; €) & yen; ¥ RMB)©& Copy; copyright symbol) & #8220; "double quotation marks on the left) & #8221;" double quotation marks on the right), such as HTML Entity elements. It creates text literally instead of the symbols you need.

 
 
  1. <script  type="text/javascript"> 
  2. window.onload=function(){  
  3.  var x=document.createTextNode("&copy; Copyrights reserved");  
  4.  document.getElementById("test").appendChild(x);  
  5. }  
  6. </script>  

However, we can use innerHTML instead:

 
 
  1. <script  type="text/javascript"> 
  2. window.onload=function(){   
  3. document.getElementById("test").innerHTML="&copy; 
  4. Copyrights reserved";  
  5. }  
  6. </script>  

We will discuss the usage of innerHTML attributes as a topic in the next section.

CloneNode ()

The cloneNode () method clones a node, that is, it can make a perfect copy of the node, so that you can insert it into the document tree later. Navigation bar HTML code:

 
 
  1. <Div id = "menu">
  2. <H1> my navigation bar
  3. <Ul id = "nav">
  4. <Li> <a href = "#"> HOME </a> </li>
  5. <Li> <a href = "#"> (X) Html/Css </a> </li>
  6. <Li> <a href = "#"> Ajax/RIA </a> </li>
  7. <Li> <a href = "#"> GoF </a> </li>
  8. <Li> <a href = "#"> JavaScript </a> </li>
  9. <Li> <a href = "#"> JavaWeb </a> </li>
  10. <Li> <a href = "#"> jQuery </a> </li>
  11. <Li> <a href = "#"> MooTools </a> </li>
  12. <Li> <a href = "#"> Python </a> </li>
  13. <Li> <a href = "#"> Resources </a> </li>
  14. </Ul>
  15. </Div>

Test cloneNode ()

 
 
  1. <script  type="text/javascript"> 
  2. window.onload=function(){  
  3.      var nav_list=[];  
  4.   var nav=document.getElementById("nav");  
  5.      navnav_list=nav.getElementsByTagName("li");  
  6.   var x=nav_list[0];  
  7.   var y=x.cloneNode(true);  
  8.   nav.appendChild(y);  
  9. }  
  10. </script> 

To use cloneNode () correctly, you must understand its two features:

1. cloneNode () accepts a parameter with an optional value of true or false. True indicates the clone element and all its subnodes. False indicates the child node that clones an element but does not contain it. In general, we use true in practice. I have never encountered the situation of cloning a node but not including it.

2. cloneNode () does not clone the event handler. This is quite annoying. I don't know why this method is defined.) So every time you clone a node, You have to redefine the event handler on the clone.

Address: http://cssrainbow.cn/tutorials/javascript/589.html

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.