zsydgithub 2 years ago
parent
commit
57edbf2426
2 changed files with 119 additions and 0 deletions
  1. 71 0
      12_垂直导航.html
  2. 48 0
      13_节点操作.html

+ 71 - 0
12_垂直导航.html

@@ -0,0 +1,71 @@
+<!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>
+    * {
+      margin: 0;
+      padding: 0;
+    }
+
+    ul {
+      list-style: none;
+    }
+
+    h2 {
+      width: 300px;
+      height: 40px;
+      background: #000;
+      color: white;
+    }
+
+    ul {
+      display: none;
+    }
+  </style>
+</head>
+
+<body>
+  <div id="container">
+    <h2>管理区</h2>
+    <ul>
+      <li>111</li>
+      <li>111</li>
+      <li>111</li>
+    </ul>
+    <h2>交流区</h2>
+    <ul>
+      <li>222</li>
+      <li>222</li>
+      <li>222</li>
+    </ul>
+  </div>
+  <script>
+    var h2 = document.getElementsByTagName('h2')
+    console.log(h2)
+    for (var i = 0; i < h2.length; i++) {
+      h2[i].onclick = function () {
+        var ul1 = next(this)
+        console.log(ul1)
+        if (ul1.style.display == 'block') {
+          ul1.style.display = 'none'
+        } else {
+          ul1.style.display = 'block'
+        }
+
+      }
+    }
+    function next(elem) {
+      do {
+        elem = elem.nextSibling
+      } while (elem.nodeType != 1)
+      return elem
+    }
+  </script>
+</body>
+
+</html>

+ 48 - 0
13_节点操作.html

@@ -0,0 +1,48 @@
+<!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">66666</p>
+    <p>456</p>
+  </div>
+  <script>
+    var div1 = document.getElementById('div1')
+    var a = document.createElement('p')
+    var b = document.createTextNode('这是一个新的文本')
+    //createElement  创建一个元素节点
+    //createTextNode  创建一个文本节点
+    console.log(b)
+    console.log(a)
+    a.innerHTML = '123' //<p>123</p>
+    // console.log(a)
+
+    //appendChild 插入节点
+    // a.appendChild(b)
+    // console.log(a)
+
+
+    //appendChild  向html DOM 添加新元素  必须首先创建这个元素节点 然后将其插入到某个位置
+    // div1.appendChild(a)
+
+    //insertBefore(参数1,参数2) 向参数2前面添加参数1
+    var p1 = document.getElementById('p1')
+    // div1.insertBefore(a,p1)
+
+    //元素节点.remove()在页面中 删除元素节点
+    // p1.remove()
+
+    //父节点.removeChild(子节点)
+    // div1.removeChild(p1)
+
+
+    //父节点.replaceChild(新节点,旧节点)
+    div1.replaceChild(a,p1)
+  </script>
+</body>
+</html>