fengchuanyu hai 1 semana
pai
achega
ee05dca9cf

+ 111 - 0
4_BOM&DOM/15_事件机制.html

@@ -0,0 +1,111 @@
+<!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-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 box1 = document.querySelector(".box1");
+        var box2 = document.querySelector(".box2");
+        var box3 = document.querySelector(".box3")
+        // 绑定事件
+        box1.onclick = function(){
+            console.log("box1");
+        }
+        box2.onclick = function(event){
+            console.log("box2");
+            // 阻止事件冒泡 stopPropagation()方法
+            event.stopPropagation();
+        }
+        box3.onclick = function(event){
+            console.log("box3");
+            // 阻止事件冒泡 stopPropagation()方法
+            // event.stopPropagation();
+        }
+
+
+        // 阻止事件默认行为
+        var doc = document.documentElement;
+        doc.oncontextmenu = function(event){
+            // return false;
+            // 阻止事件默认行为 preventDefault()方法
+            event.preventDefault();
+            console.log(event);
+        }
+
+        // 事件冒泡: 默认情况下事件触发从当前元素开始逐层向外依次触发
+
+
+        // addEventListener 后面有三个参数 第一个事件名称 第二个事件处理函数 第三个参数是布尔值 如果为true 则事件捕获 否则事件冒泡
+        
+        // 事件冒泡: 事件从当前元素开始逐层向外依次触发
+        // 事件捕获: 事件从当前元素开始逐层向内元素依次触发
+
+        // addEventListener 可以为一个元素绑定多次事件
+
+
+        // box1.addEventListener("click",function(){
+        //     console.log("box1");
+        // },true)
+        // box2.addEventListener("click",function(){
+        //     console.log("box2");
+        // },true)
+        // box3.addEventListener("click",function(){
+        //     console.log("box3");
+        // },true)
+
+
+        // box1.addEventListener("click",function(){
+        //     console.log("box1");
+        // },false)
+        // box2.addEventListener("click",function(){
+        //     console.log("box2");
+        // },false)
+        // box3.addEventListener("click",function(){
+        //     console.log("box3");
+        // },false)
+
+        // 当同时触发捕获和冒泡那么 先执行捕获再执行冒泡
+
+        // 清除事件
+        // 如果使用的是on 绑定的事件本质就是赋值操作只要将事件处理函数赋值为null 就可以清除事件了
+        // box1.onclick = function(){
+        //     console.log("box1");
+        // }
+        // box1.onclick = null;
+
+        // function foo(){
+        //     console.log("box1");
+        // }
+        // box1.addEventListener("click",foo)
+
+        // 如果使用的是addEventListener 绑定的事件可以用removeEventListener 方法来清除事件的(注:removeEventListener 方法的参数必须和addEventListener内的参数一样)
+
+        // box1.removeEventListener("click",foo)
+    </script>
+</body>
+</html>

+ 34 - 0
4_BOM&DOM/16_事件委托.html

@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <ul>
+        <li>a</li>
+        <li>b</li>
+        <li>c</li>
+        <li>d</li>
+        <li>e</li>
+        <li>f</li>
+    </ul>
+    <script>
+        // var li = document.querySelectorAll("li");
+        // for(var i=0;i<li.length;i++){
+        //     li[i].addEventListener("click",function(){
+        //         console.log(this.innerText);
+        //     })
+        // }
+
+
+        // 事件委托 利用事件冒泡机制将事件绑定到父元素上 点击子元素时 事件会冒泡到父元素上 触发事件处理函数
+        var ul = document.querySelector("ul");
+        ul.addEventListener("click",function(event){
+            // target 表示单签元素内是哪一个触发的事件
+            console.log(event.target.innerText);
+        })
+    </script>
+</body>
+</html>

+ 36 - 0
4_BOM&DOM/17_其他事件.html

@@ -0,0 +1,36 @@
+<!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: 200px;
+            height: 200px;
+            background-color: red;
+        }
+    </style>
+</head>
+<body>
+    <div id="box"></div>
+    <button id="btn">点击我</button>
+    <script>
+        // 获取元素
+        var box = document.querySelector("#box");
+        var btn = document.querySelector("#btn");
+        // 鼠标按下事件
+        btn.onmousedown = function(){
+            console.log("鼠标按下");
+        }
+        // 鼠标松开事件
+        btn.onmouseup = function(){
+            console.log("鼠标松开");
+        }
+        // 鼠标移动事件
+        box.onmousemove = function(){
+            console.log("鼠标移动");
+        }
+    </script>
+</body>
+</html>

+ 30 - 0
4_BOM&DOM/18_offse.html

@@ -0,0 +1,30 @@
+<!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: 200px;
+            height: 200px;
+            background-color: red;
+            position: absolute;
+            top: 200px;
+            left: 100px;
+        }
+    </style>
+</head>
+<body>
+    <div class="box"></div>
+    <script>
+        // 获取元素
+        var box = document.querySelector(".box");
+        // 获取元素的偏移量
+        // offsetLeft属性:元素定位后 元素的左偏移量
+        console.log(box.offsetLeft);
+        // offsetTop属性:元素定位后 元素的上偏移量
+        console.log(box.offsetTop);
+    </script>
+</body>
+</html>