Creation, substitution, deletion, insertion of JavaScript nodes

Source: Internet
Author: User

1. Creation of nodes

Nodes are created using document.createlment (); text nodes are created using Document.creattextnode (); If you want to add <li> cantaloupe </li> to a list like the one shown below

<ul id = "Fruit" >    <li> Apple </li>    <li> Orange </li>    <li> watermelon </li>    <li> Pear </li></ul>

You can add the following JavaScript code

var frnode = document.getElementById ("fruit"); var liNode = createlement ("li"); var hmgnode = Creattextnode ("cantaloupe"); Linode.appendchild (Hmgnode); Frnode.appendchild (LiNode) ;

2. Replacement of nodes

The substitution of a node can use Parentnode.replacechild (Newnode,oldnode), where the OldNode is the node that will be replaced, NewNode is the replacement node, and ParentNode is the parent of the OldNode. The sample code is as follows

<ul id = "Fruit" >    <li id = "Apple" > Apple </li>    <li id = "Orange" > Orange </li>    <li > Watermelon </li>    <li> pears </li></ul>
<ul id = "Food" >    <li id = "Biscuit" > Cookies </li>    <li> rice </li>    <li> Beef </ li>    <li> Noodles </li></ul>

You can add the following JavaScript code

var frnode = document.getElementById ("fruit"); var apnode = document.getElementById ("apple"); var binode = Document.getelemetnbyid ("biscuit"); Frnode.replace (Binode,apnode);

At this point the Apple is replaced by a cookie, and the cookie is removed from the food list.

3. Deletion of nodes

Node deletion using RemoveChild (), here is an easy way to delete a node, for example, to delete the apple tag from the fruit list above, then

var apnode = document.getElementById ("apple"); ApNode.parentNode.removeChild (Apnode);

This means that to delete a node, you can use the RemoveChild () method of the parent node of the node to remove the node.

4. Insertion of nodes

The insertion of a node uses the InsertBefore (Newnode,nextnode) method, which is to insert a new node before a node. For example, if we want to add cookies from the food list to the fruit list,

var frnode = document.getElementById ("fruit"); var ornode = document.getElementById ("orange"); var binode = Document,getelementbyid ("biscuit"); Frnode.insertbefore (Binode,ornode);

This cookie is added to the fruit list and is also deleted from the food list.

Creation, substitution, deletion, insertion of JavaScript nodes

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.