|
|
@@ -0,0 +1,78 @@
|
|
|
+<!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: 600px;
|
|
|
+ height: 600px;
|
|
|
+ background-color: blue;
|
|
|
+ }
|
|
|
+ .box2{
|
|
|
+ width: 400px;
|
|
|
+ height: 400px;
|
|
|
+ background-color: red;
|
|
|
+ }
|
|
|
+ .box3{
|
|
|
+ width: 200px;
|
|
|
+ height: 200px;
|
|
|
+ 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(){
|
|
|
+ // console.log("box3");
|
|
|
+ // }
|
|
|
+
|
|
|
+
|
|
|
+ oBox1.addEventListener("click",function(){
|
|
|
+ console.log("box1");
|
|
|
+
|
|
|
+ })
|
|
|
+ oBox2.addEventListener("click",function(){
|
|
|
+ console.log("box2");
|
|
|
+ })
|
|
|
+ oBox3.addEventListener("click",function(event){
|
|
|
+ console.log("box3");
|
|
|
+ // 阻止事件冒泡
|
|
|
+ event.stopPropagation();
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ // addEventListener 第三个参数为布尔值 默认值为false事件冒泡 true为事件捕获
|
|
|
+ // 事件捕获 事件触发顺序是由外向内
|
|
|
+ // oBox1.addEventListener("click",function(){
|
|
|
+ // console.log("box1");
|
|
|
+ // },true)
|
|
|
+ // oBox2.addEventListener("click",function(){
|
|
|
+ // console.log("box2");
|
|
|
+ // },true)
|
|
|
+ // oBox3.addEventListener("click",function(){
|
|
|
+ // console.log("box3");
|
|
|
+ // },true)
|
|
|
+
|
|
|
+ // 如果同时触发捕获和冒泡那么会先处理捕获事件然后处理冒泡事件
|
|
|
+
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|