zsydgithub 1 anno fa
parent
commit
10fee07bea
3 ha cambiato i file con 149 aggiunte e 0 eliminazioni
  1. 42 0
      Dom/12_节点操作.html
  2. 85 0
      Dom/13_事件冒泡.html
  3. 22 0
      Dom/14_阻止事件默认行为.html

+ 42 - 0
Dom/12_节点操作.html

@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+<body>
+  <div id="div1">
+    <p id="p1">123</p>
+    <p>456</p>
+  </div>
+  <script>
+    var a = document.createElement('p')
+    var b = document.createTextNode('789')
+    console.log(a)
+    console.log(b)
+
+    a.appendChild(b)
+    console.log(a)
+    //appendChild 向html里面添加新元素  必须先创建元素接待你 然后追加
+    var div1 = document.getElementById('div1')
+    // div1.appendChild(a)
+
+    var p1 = document.getElementById('p1')
+    //insertBefore(参数1,参数2)  向参数2前面添加参数1
+    // div1.insertBefore(a,p1)
+    
+
+    //xx.remove()在页面中删除xx节点
+    // p1.remove()
+
+
+    //xx.removeChild()移除XX里面的子节点
+    // div1.removeChild(p1)
+
+    //xx.replaceChild(新节点,旧节点)
+    div1.replaceChild(a,p1)
+  </script>
+</body>
+</html>

+ 85 - 0
Dom/13_事件冒泡.html

@@ -0,0 +1,85 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+  <style>
+    #div1{
+      width: 300px;
+      height: 300px;
+      background: antiquewhite;
+    }
+    #div2{
+      width: 200px;
+      height: 200px;
+      background: red;
+    }
+    #div3{
+      width: 100px;
+      height: 100px;
+      background: green;
+    }
+  </style>
+</head>
+<body>
+  <div id="div1">
+    <div id="div2">
+      <div id="div3"></div>
+    </div>
+  </div>
+  <script>
+    var div1 = document.getElementById('div1')
+    var div2 = document.getElementById('div2')
+    var div3 = document.getElementById('div3')
+    // div1.onclick = function(){
+    //   console.log('div1')
+    // }
+    // div2.onclick = function(e){
+    //   console.log('div2')
+    //   // e.preventDefault()
+    //   //阻止事件冒泡
+    //   e.stopPropagation()
+    // }
+    // div3.onclick = function(){
+    //   console.log('div3')
+    //   //阻止事件冒泡
+    //   event.cancelBubble = true
+    // }
+    //事件冒泡  从里到外执行
+
+
+      /* 
+        参数1  事件名称 字符串 必填
+        参数2   执行函数  必填
+        参数3   布尔值
+      */
+      div1.addEventListener('click',function(){
+        console.log('div1')
+      },false)
+      div2.addEventListener('click',function(){
+        console.log('div2')
+      },false)
+      div3.addEventListener('click',function(){
+        console.log('div3')
+      },false)
+      div1.addEventListener('click',function(){
+        console.log('div1')
+      },true)
+      div2.addEventListener('click',function(){
+        console.log('div2')
+      },true)
+      div3.addEventListener('click',function(){
+        console.log('div3')
+      },true)
+
+
+      /*  
+        从里到外 事件冒泡  fasle
+        从外到里 事件捕获  true
+        先捕获 后冒泡 先从外到里 然后从里到外
+      */
+  </script>
+</body>
+</html>

+ 22 - 0
Dom/14_阻止事件默认行为.html

@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+<body>
+  <a id="a1" href="https://www.baidu.com">跳转</a>
+  <script>
+    var a1 = document.getElementById('a1')
+    a1.onclick = function(e){
+      console.log(123)
+      //阻止事件的默认行为  仅W3C
+      // e.preventDefault()
+      //设置false表示取消默认行为  
+      e.returnValue = false  //适配于ie浏览器
+    }
+  </script>
+</body>
+</html>