Home > Article > Web Front-end > How to get elements and delete them in javascript
Javascript method to get and delete elements: First use the "document.querySelector(element name)" or "document.getElementById(id value)" statement to get the element object; then use "element object.remove()" Statement to delete elements.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript method to get and delete elements
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <div id="div" style="border: 2px dashed #006DAF;padding: 10px;">div元素</div> <input id="btn" type="button" value="删除子节点"> </body> <script> function deleteChild() { // var div = document.querySelector("div"); var div=document.getElementById("div"); div.remove(); } var btn = document.getElementById("btn").onclick = function() { deleteChild(); } </script> </html>
Rendering:
Description:
# The ##querySelector() method returns an element in the document that matches the specified CSS selector. The getElementById() method returns a reference to the first object with the specified ID. Theremove() method can be used to delete all elements on the parent node, including all text and child nodes. [Related recommendations:The above is the detailed content of How to get elements and delete them in javascript. For more information, please follow other related articles on the PHP Chinese website!