How to delete dom in javascript: First create an HTML sample file and create a button button; then delete the node by using the "removeChild()" method.
The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
JS Implementation of DOM Delete Node Operation Example
1. Introduction
Deleting nodes is achieved by using the removeChild() method.
removeChild() method is used to delete a child node.
obj. removeChild(oldChild)
oldChild: Indicates the node that needs to be deleted.
2. Use
to delete nodes. This example will dynamically delete the selected text on the page through the removeChild() method of the DOM object.
3. Code
<!DOCTYPE html> <html> <head> <title>删除节点</title> <script language="javascript"> <!-- function delNode() { var deleteN=document.getElementById('di'); if(deleteN.hasChildNodes()) { deleteN.removeChild(deleteN.lastChild); } } --> </script> </head> <body> <h1>删除节点</h1> <div id="di"> <p>第一行文本</p> <p>第二行文本</p> <p>第三行文本</p> </div> <form> <input type="button" value="删除" onclick="delNode();"/> </form> </body> </html>
[Recommended learning: js basic tutorial]
The above is the detailed content of How to delete dom in javascript. For more information, please follow other related articles on the PHP Chinese website!