Solving event bubbling

<!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 (); // will even (event) as a parameter, this is the "click" event, through this even, you can get to event.target.stopPropagation () to solve the event bubbling
 console.log("a"); }
</script>

 

Guess you like

Origin www.cnblogs.com/yjbyjb/p/11904668.html