<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width:80px;
height:80px;
border:1px solid #000;
}
</style>
</head>
<body>
<div class="aaa" id="p1">
<span>111</span>
</div>
<div class="aaa" id="p2">
<span>333</span>
</div>
<div class="aaa" id="p3">
<span>2222</span>
</div>
</body>
</html>
3 回答
已采纳
千秋此意
TA贡献158条经验 获得超187个赞
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <style> div { width: 80px; height: 80px; border: 1px solid #000; } </style> </head> <body> <div id="p1"> <span>111</span> </div> <div id="p2"> <span>333</span> </div> <div id="p3"> <span>2222</span> </div> </body> <script> var divs = document.querySelectorAll('div'); for (var i = 0; i < divs.length; i++) { divs[i].onclick = function() { this.parentNode.removeChild(this); } } </script> </html>
信者得救
TA贡献22条经验 获得超10个赞
document.body.addEventListener('click', function(event) { if (event.target.nodeName === 'DIV') { event.target.parentNode.removeChild(event.target); } });
添加回答
举报
0/150
提交
取消