Home  >  Article  >  Web Front-end  >  How to get elements and delete them in javascript

How to get elements and delete them in javascript

青灯夜游
青灯夜游Original
2021-06-22 12:50:402926browse

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.

How to get elements and delete them in javascript

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:

How to get elements and delete them in javascriptDescription:

# 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:

javascript learning tutorial]

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn