1 nodeType 节点类型

  • nodeType == 1 元素节点
  • nodeType == 2 属性节点
  • nodeType == 3 文本节点 ``` // getAttributeNode() 获取属性节点 // firstChild() 获取第一个子节点

    hello world

  1. <a name="6yz3H"></a>
  2. ## 2 获取父子兄弟节点
  3. - parentNode
  4. - childNodes
  5. - children
  6. - firstChild firstElementChild lastChild lastElementChild
  7. - previousSibling previousElementSibling
  8. - nextSibling nextElementSibling
  9. ```javascript
  10. <ul id="app">
  11. <li>hello world</li>
  12. <li>hello world</li>
  13. <li>hello world</li>
  14. <li>hello world</li>
  15. </ul>
  16. <script>
  17. var app = document.getElementById("app")
  18. var childs = app.childNodes;
  19. console.log(childs); // 所有的子节点 共有9个
  20. var arr = []
  21. for(var i = 0;i<childs.length;i++){
  22. if(childs[i].nodeType == 1){
  23. arr.push(childs[i])
  24. }
  25. }
  26. console.log(arr);
  27. </script>
  1. <ul id="app">
  2. <li>hello world</li>
  3. <li>hello world</li>
  4. <li>hello world</li>
  5. <li>hello world</li>
  6. </ul>
  7. <script>
  8. var app = document.getElementById("app")
  9. console.log(app.children) // 只获取元素节点
  10. </script>

3 增加节点

3-1 appendChild 追加节点(末尾添加节点)

  1. createElement() 创建节点
  2. createTextNode() 创建文本节点
  1. <div id="app">
  2. </div>
  3. <script>
  4. var app = document.getElementById("app")
  5. var p = document.createElement("p")
  6. var txt = document.createTextNode("hello world")
  7. p.appendChild(txt)
  8. console.log(p)
  9. app.appendChild(p)
  10. console.log(app)
  11. </script>

3-2 insertBefore 插入节点

parentNode.insertBefore(newNode,targetNode)

  1. <ul id="app">
  2. <li>html</li>
  3. <li id="js">javascript</li>
  4. </ul>
  5. <!--
  6. innerHtml 元素里的文字
  7. parentNode.insertBefore(newNode,在谁前面) 插入节点
  8. -->
  9. <script>
  10. var app = document.getElementById("app")
  11. var li = document.createElement("li")
  12. var js = document.getElementById("js")
  13. li.innerHTML = "vue"
  14. console.log(li);
  15. app.insertBefore(li,js)
  16. console.log(app)
  17. </script>

3-3 append 在结尾插入节点

3-4 prepend 在开头插入节点

  1. <ul id="app">
  2. <li>html</li>
  3. </ul>
  4. <script>
  5. // append,prepend
  6. var app = document.getElementById("app");
  7. var p1 = "<h1>我是在结尾插入的</h1>";
  8. var p2 = "<h1>我是在开头插入的</h1>"
  9. app.append(p1);
  10. app.prepend(p2)
  11. </script>

3-5 before

3-6 after

  1. <p id="p">hello world</p>
  2. <script>
  3. var p = document.getElementById("p");
  4. p.after("after")
  5. p.before("before")
  6. </script>

4 删除节点

4-1 removeChild() 删除节点

  1. <div id="app">
  2. <div id="child">
  3. hello world
  4. </div>
  5. </div>
  6. <script>
  7. // removeChild() 让元素从DOM树,渲染树中消失
  8. var app = document.getElementById("app")
  9. var child = document.getElementById("child")
  10. app.removeChild(child)
  11. </script>

5 替换节点

5-1 replaceChild 替换节点

parentNode.replaceChild(newNode,targetNode)

  1. <div id="app">
  2. <div id="child">
  3. child
  4. </div>
  5. </div>
  6. <script>
  7. var app = document.getElementById("app")
  8. var child = document.getElementById("child")
  9. var p = document.createElement("p")
  10. p.innerHTML = "hello world"
  11. app.replaceChild(p,child)
  12. console.log(app);
  13. </script>

6 克隆节点

6-1 cloneNode 克隆节点

  1. node.cloneNode( )
  2. 括号为空或false浅拷贝,只复制标签不复制内容
  3. 括号为true,深拷贝,也复制内容
  1. <div id="app">
  2. <div id="one">hello world</div>
  3. </div>
  4. <script>
  5. var app = document.getElementById("app")
  6. var one = document.getElementById("one")
  7. var test = one.cloneNode(true)
  8. console.log(test);
  9. app.appendChild(test);
  10. console.log(app);
  11. </script>