CodeLab
Show Output
 
xxxxxxxxxx
23
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="utf-8">
5
    <title>JavaScript Remove an Element from the DOM</title>
6
</head>
7
<body>
8
    <div id="main">
9
        <h1 id="title">Hello World!</h1>
10
        <p id="hint">This is a simple paragraph.</p>
11
    </div>
12
    
13
    <button type="button" onclick="removeElement()">Remove Element</button>
14
    
15
    <script>
16
    function removeElement() {
17
        var parentElem = document.getElementById("main");
18
        var childElem = document.getElementById("hint");
19
        parentElem.removeChild(childElem);
20
    }
21
    </script>
22
</body>
23
</html>
 

在微博、微信、公众号、小程序分享此示例。 如果您觉得有帮助,请给我们一个赞。