Reproduced Javascript removechild () can not remove all child nodes of the workaround

Source: Internet
Author: User

In JavaScript, only one way to delete a node is provided: RemoveChild ().
The RemoveChild () method is used to delete a child node of the parent node.

Grammar:

Parent.removechild (Thisnode)

Parameter description:

Parameters Description
Thisnode The current node, which is the node to be deleted
Parent The parent node of the current node, which is Thisnode.parentnode

For example, the statement for deleting a node that id= "Demo" is:

1 var thisnode=document.getelementbyid ("demo");

2 thisNode.parentNode.removeNode (thisnode);
For example, delete a node:

1 <DivID= "Demo">2 <DivID= "Thisnode">Click Delete my</Div>3 </Div>4 <Scripttype= "Text/javascript">5 document.getElementById ("Thisnode"). onclick=function(){6  This. Parentnode.removechild ( This);7 }

As you can see, JavaScript only provides a way to delete nodes, but it's enough.
Ps:javascript How to delete a child node
The HTML code is as follows:

<DivID= "F"> <Div>A</Div> <Div>B</Div> <Div>C</Div> </Div>

If you want to delete all the child nodes under the F-node, it is natural and normal to think that the method should be the following code:

1 var f = document.getElementById ("f"2var childs =3 for (var i = 0; i < childs.length; i++456 }      

When the program ran, we found that no matter in Firefox or under IE, can not completely delete all the sub-nodes (in Firefox, the blank area is also
As a node, so the result of deleting the node will be different), this is because when you put the index of 0 sub-node after deletion so natural original index
The 1 node at this time its index becomes 0, and then the variable i has become 1, the program continues to delete the original index of 2 is now 1 node,
The result of this program is that only half of the child nodes are deleted, and the result is the same with the for-in traversal. Want to delete all the nodes normally
, we should remove it from behind, the code is as follows:

 for (var i = childs.length-1; I >= 0; i--) {   alert (childs[i].nodename);   F.removechild (Childs[i]); }

We start by deleting the index maximum, using a descending method so that the index does not move and change.

Original address:

Javascript RemoveChild () Methods for deleting nodes and deleting child nodes

Reproduced Javascript removechild () can not remove all child nodes of the workaround

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.