e 1 an în urmă
părinte
comite
169387701a
2 a modificat fișierele cu 105 adăugiri și 0 ștergeri
  1. 38 0
      JS初级/DOM/21事件源.html
  2. 67 0
      JS初级/DOM/22.事件源.html

+ 38 - 0
JS初级/DOM/21事件源.html

@@ -0,0 +1,38 @@
+<!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>
+        #box {
+            width: 300px;
+            height: 300px;
+            background: #00f;
+        }
+        #box1 {
+            width: 150px;
+            height: 150px;
+            background: #0ff;
+        }
+    </style>
+</head>
+<body>
+    <!-- 事件源:事件的源头 -->
+    <div id="box">
+        <div id="box1"></div>
+    </div>
+    <script>
+        var box = document.getElementById("box");
+        var box1 = document.getElementById("box1");
+        box.onclick = function(event){
+            console.log(event);
+            // alert("111");
+        }
+        box1.onclick = function(event) {
+            // alert("222");
+            event.stopPropagation();
+        }
+    </script>
+</body>
+</html>

+ 67 - 0
JS初级/DOM/22.事件源.html

@@ -0,0 +1,67 @@
+<!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: 300px;
+            height: 300px;
+            background: #f00;
+            margin-top: 30px;
+        }
+        #box2 {
+            width: 150px;
+            height: 150px;
+            background: #ff0;
+        }
+        #box3 {
+            width: 300px;
+            height: 300px;
+            background: #00f;
+            margin-top: 30px;
+        }
+        #box4 {
+            width: 150px;
+            height: 150px;
+            background: #0f0;
+        }
+    </style>
+</head>
+<body>
+    <!-- 
+        万维网联盟(W3C)
+        DOM事件流:三个阶段
+            事件捕获
+            目标阶段
+            事件冒泡
+    -->
+    <div id="box1">
+        <div id="box2"></div>
+    </div>
+    <div id="box3">
+        <div id="box4"></div>
+    </div>
+    <script>
+        var box1 = document.getElementById("box1");
+        var box2 = document.getElementById("box2");
+        var box3 = document.getElementById("box3");
+        var box4 = document.getElementById("box4");
+
+        box1.addEventListener("click",function(){
+            console.log(this,'box1')
+        },true)
+        box2.addEventListener("click",function(){
+            console.log(this,'box2')
+        },true)
+        box3.addEventListener("click",function(){
+            console.log(this,'box3')
+        },false)
+        box4.addEventListener("click",function(){
+            console.log(this,'box4')
+        },false)
+        // removeEventListener('监听的时间方法',执行的函数,触发的事件类型:true (事件捕获) / false(事件冒泡))
+    </script>
+</body>
+</html>