Javascript learning note (19th) node operation implementation code _ basic knowledge

Source: Internet
Author: User
Javascript learning note node operation implementation code, including creating, adding, removing, replacing, and copying nodes.

The Code is as follows:



  • Project 1

  • Project 2

  • Project 3



1. Create an element node
The document. createElement () method is used to create an element. It accepts a parameter, that is, the tag name of the element to be created, and returns the created element node.

The Code is as follows:


Var p = document. createElement ("p"); // create a p element
P. id = "myDiv"; // set the id of p.
P. className = "box"; // set the class of p.


After creating an element, add it to the document tree.

2. Add element nodes
The appendChild () method is used to add a node to the end of the childNodes list and return the element node to be added.

The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Var li = document. createElement ("li"); // create a li
Li. innerHTML = "Project 4"; // Add text to li
Ul. appendChild (li); // Add li to the end of the ul subnode


After adding:

The Code is as follows:



  • Project 1

  • Project 2

  • Project 3

  • Project 4



The appendChild () method can also add existing elements, which will move the elements from the original location to the new location.

The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Ul. appendChild (ul. firstChild); // move the first element node of ul to the end of the ul subnode


After running (IE ):

The Code is as follows:



  • Project 2

  • Project 3

  • Project 1



InsertBefore () method. If you want to insert a node not at the end but at a specific position, this method accepts two parameters. The first method is the node to be inserted, the second is the reference node, which returns the element node to be added.

The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Var li = document. createElement ("li"); // create a li
Li. innerHTML = "Project 4"; // Add text to li
Ul. insertBefore (li, ul. firstChild); // Add li to the front of the first subnode of ul.


After adding:

The Code is as follows:



  • Project 4

  • Project 1

  • Project 2

  • Project 3



The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Var li = document. createElement ("li"); // create a li
Li. innerHTML = "Project 4"; // Add text to li
Ul. insertBefore (li, ul. null); // Add li to the end of the ul subnode


After adding:

The Code is as follows:



  • Project 1

  • Project 2

  • Project 3

  • Project 4



The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Var li = document. createElement ("li"); // create a li
Li. innerHTML = "Project 4"; // Add text to li
Var lis = ul. getElementsByTagName ("li") // obtain the set of all li in ul
Ul. insertBefore (li, lis [1]); // Add li to the front of the second li node in ul


After adding:

The Code is as follows:



  • Project 1

  • Project 4

  • Project 2

  • Project 3



3. Remove element nodes
The removeChild () method is used to remove a node. It accepts a parameter, that is, the node to be removed, and returns the removed node. Note that the removed node is still in the document, however, there is no location in the document.

The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Var fromFirstChild = ul. removeChild (ul. firstChild); // remove ul's first subnode 1 var ul = document. getElementById ("myList"); // obtain ul
Var lis = ul. getElementsByTagName ("li") // obtain the set of all li in ul
Ul. removeChild (lis [0]); // remove the first li. Different from the preceding, consider the differences between browsers.


4. Replace element nodes
The replaceChild () method is used to replace nodes. Two parameters are accepted. The first parameter is the node to be inserted, the second parameter is the node to be replaced, and the replaced node is returned.

The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Var fromFirstChild = ul. replaceChild (ul. firstChild); // replace ul's first subnode 1 var ul = document. getElementById ("myList"); // obtain ul;
Var li = document. createElement ("li"); // create a li
Li. innerHTML = "Project 4"; // Add text to li
Var lis = ul. getElementsByTagName ("li") // obtain the set of all li in ul
Var returnNode = ul. replaceChild (li, lis [1]); // replace the original second li with the created li


5. Copy a node
The cloneNode () method is used to copy a node and accepts a Boolean parameter. true indicates deep replication (copying a node and all its subnodes), and false indicates light replication (copying a node itself, do not copy subnodes)

The Code is as follows:


Var ul = document. getElementById ("myList"); // obtain ul
Var deepList = ul. cloneNode (true); // deep copy
Var shallowList = ul. cloneNode (false); // shallowList


Note the differences between IE and other browsers for node operations (as described in section 18th)
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.