In Javascript, there is only one method for removing nodes: removeChild().
The removeChild() method is used to remove a child node of the parent node.
Syntax: parent.removeChild(thisNode) parameter description:
thisNode | The current node, the node to be deleted |
parent | The parent node of the current node, i.e. thisNode.parentNode |
For example, the statement to delete the node with id="demo" is:
- var thisNode=document.getElementById("demo");
- thisNode.parentNode.removeNode(thisNode);
For example, to delete a node:
- <div id="demo">
- < div id = "thisNode" > click to delete me </ div >
- </div>
- <script type="text/javascript">
- document.getElementById("thisNode").onclick=function(){
- this.parentNode.removeChild(this);
- }
- </script>
Example demonstration:
click delete me
It can be seen that although Javascript only provides a method to delete nodes, it is enough.