3个相同的div,怎么获取到点击的div,并删除他?JS

<!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>


19990000
浏览 1948回答 3
3回答

千秋此意

<!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>

信者得救

    document.body.addEventListener('click', function(event) {         if (event.target.nodeName === 'DIV') {             event.target.parentNode.removeChild(event.target);         }     });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript