e 2 weeks ago
parent
commit
bba8992f7c
2 changed files with 84 additions and 21 deletions
  1. 43 21
      3.js初级/DOM/14.垂直导航.html
  2. 41 0
      3.js初级/DOM/19.事件委托.html

+ 43 - 21
3.js初级/DOM/14.垂直导航.html

@@ -1,27 +1,49 @@
 <!DOCTYPE html>
 <html lang="en">
-<head>
-    <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
     <title>Document</title>
-</head>
-<body>
+    <style>
+      ul {
+        display: none;
+      }
+    </style>
+  </head>
+  <body>
     <div id="container">
-        <h1>管理区</h1>
-        <ul>
-            <li>你好你好1</li>
-            <li>你好你好2</li>
-            <li>你好你好3</li>
-            <li>你好你好4</li>
-        </ul>
-        <h1>项目区</h1>
-        <ul>
-            <li>开心开心1</li>
-            <li>开心开心2</li>
-            <li>开心开心3</li>
-            <li>开心开心4</li>
-        </ul>
+      <h1>管理区</h1>
+      <ul>
+        <li>你好你好1</li>
+        <li>你好你好2</li>
+        <li>你好你好3</li>
+        <li>你好你好4</li>
+      </ul>
+      <h1>项目区</h1>
+      <ul>
+        <li>开心开心1</li>
+        <li>开心开心2</li>
+        <li>开心开心3</li>
+        <li>开心开心4</li>
+      </ul>
     </div>
     <!-- 获取所有的h1标签 对h1标签进行点击事件 -->
-</body>
-</html>
+    <script>
+      var container = document.querySelector("#container");
+      console.log(container.children);
+      var h1 = document.querySelectorAll("h1");
+      for (var i = 0; i < h1.length; i++) {
+        h1[i].onclick = function () {
+          console.log(this);
+          var uls = this.nextElementSibling;
+          console.log(uls, "uls");
+          if (uls.style.display == "none") {
+            uls.style.display = "block";
+          } else {
+            uls.style.display = "none";
+          }
+        };
+      }
+    </script>
+  </body>
+</html>

+ 41 - 0
3.js初级/DOM/19.事件委托.html

@@ -0,0 +1,41 @@
+<!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>11</li>
+        <li>22</li>
+        <li>33</li>
+        <li>44</li>
+        <p>87878</p>
+        <li>55</li>
+    </ul>
+    <button>添加</button>
+    <script>
+        var btn = document.querySelector("button")
+        var uls = document.querySelector("ul");
+        var list  = document.querySelectorAll("ul li");
+        // 事件委托:减少内存消耗 避免多次循环
+        // for(var i=0;i<list.length;i++) {
+        //     list[i].onclick = function(){
+        //         console.log(this)
+        //     }
+        // }
+        uls.onclick = function(event) {
+            if(event.target.nodeName == 'LI') {
+
+                console.log(event.target.innerText,'www')
+            }
+        }
+        btn.onclick = function() {
+            var li1 = document.createElement("li");
+            li1.innerText = '大家好';
+            uls.appendChild(li1)
+        }
+    </script>
+</body>
+</html>