Javascript removeChild () is used to delete nodes and subnodes. javascript deletes nodes.

Source: Internet
Author: User

Javascript removeChild () is used to delete nodes and subnodes. javascript deletes nodes.

The following describes how to delete a node using Javascript removeChild (). The details are as follows:

In Javascript, only one method to delete a node is provided: removeChild ().

The removeChild () method is used to delete a subnode of the parent node.

Syntax:

Parent. removeChild (thisNode)

Parameter description:

 

Parameters Description
ThisNode Current node, that is, the node to be deleted
Parent The parent node of the current node, that is, thisNode. parentNode.

For example, the statement for deleting a node with id = "demo" is:

var thisNode=document.getElementById("demo");thisNode.parentNode.removeNode(thisNode);

For example, delete a node:

<Div id = "demo"> <div id = "thisNode"> click Delete mE </div> <script type = "text/javascript"> document. getElementById ("thisNode "). onclick = function () {this. parentNode. removeChild (this) ;}</script>

Example:

 

As you can see, although Javascript only provides one method to delete nodes, It is enough.

Ps: JavaScript method for deleting subnodes

The HTML code is as follows:

<div id="f">  <div>a</div>  <div>b</div>  <div>c</div> </div> 

If you want to delete all the sub-nodes under Node f, You can naturally think of the following code:

var f = document.getElementById("f"); var childs = f.childNodes; for(var i = 0; i < childs.length; i++) {   alert(childs[i].nodeName);   f.removeChild(childs[i]); } 

After the program runs, we find that no child nodes can be completely deleted in either FireFox or IE.
As a node, so the results of deleting the node will be different.) This is because when you delete a subnode whose index is 0, the original index is natural.
When the index is set to 1, the index is changed to 0, and the variable I is changed to 1. When the program continues, it will delete the node whose index is 2 and is now 1, in this way, only half of the child nodes are deleted, and the result is also the same as that for in traversal. Delete all nodes normally
The Code is as follows:

for(var i = childs.length - 1; i >= 0; i--) {   alert(childs[i].nodeName);   f.removeChild(childs[i]); } 

We start to delete the index from the maximum value and adopt the descending method, so that the index will not change.

Articles you may be interested in:
  • Precautions for using javascript removeChild
  • Js removeChild blind-eye Error
  • Memory leakage caused by javascript removeChild
  • How to remove javascript node removeChild ()
  • Js uses the removeChild method to dynamically Delete div elements
  • Example of using javascript to delete an element node removeChild ()
  • RemoveChild () function usage in JavaScript

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.