解决事件冒泡

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style>
            #a{
                width: 100px;
                height: 100px;
                background: red;
                border: 1px solid;
                
            }
            #box{
                width: 200px;
                height: 200px;
                background: blue;
                border: 1px solid;
                
            }
        </style>
    </head>
    <body>
        <div id="box">
            <div id='a'></div>
        </div>
    </body>
</html>
<script>
    document.getElementById("box").onclick=function(){
        console.log("box");
    }
    document.getElementById("a").onclick=function(even){
         even.stopPropagation();  //将even(事件)作为参数,就是这个“点击”事件,通过这个even,可以获取到event.target.stopPropagation()解决事件冒泡
 console.log("a"); }
</script>

 

posted @ 2019-11-21 11:54  颜,  阅读(44)  评论(0编辑  收藏  举报