12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- .box1{
- width: 400px;
- height: 400px;
- background-color: red;
- }
- .box2{
- width: 200px;
- height: 200px;
- background-color: blue;
- }
- .box3{
- width: 100px;
- height: 100px;
- background-color: green;
- }
- </style>
- </head>
- <body>
- <div class="box1">
- <div class="box2">
- <div class="box3"></div>
- </div>
- </div>
- <script>
- // 事件冒泡 当有多个元素嵌套的时候 并且绑定相同事件当内层元素触发事件的时候 会逐层触发外层元素的事件
- var oBox1 = document.getElementsByClassName("box1")[0];
- var oBox2 = document.getElementsByClassName("box2")[0];
- var oBox3 = document.getElementsByClassName("box3")[0];
- oBox1.onclick = function(){
- console.log("box1");
- }
- oBox2.onclick = function(){
- console.log("box2");
- }
- oBox3.onclick = function(e){
- console.log("box3");
- // 阻止事件冒泡
- e.stopPropagation();
- }
- </script>
- </body>
- </html>
|