1 nodeType 节点类型
- nodeType == 1 元素节点
- nodeType == 2 属性节点
- nodeType == 3 文本节点
```javascript
hello world
<a name="6yz3H"></a>## 2 获取父子兄弟节点- parentNode- childNodes ---- tips:childNodes获取所有类型的节点- children ---tips:只获得子节点- firstChild firstElementChild lastChild lastElementChild- previousSibling previousElementSibling- nextSibling nextElementSibling<a name="rw8PD"></a>### 2-1.parentNode```css<body><div id="parent"><div id="child">child</div></div><script>var child = document.getElementById("child");console.log(child);var parent = child.parentNode;console.log(parent)</script></body>
2-2.childNode
<body><div id="parent"><p class="one">1</p><p class="one">2</p><p class="one">3</p></div><script>var parent = document.getElementById("parent");console.log(parent);var child = parent.childNodes;console.log(child);</script></body>
2-3第一个
<body><div id="parent"><p class="one">1</p><p class="one">2</p><p class="one">3</p></div><script>var parent = document.getElementById("parent");var childs = parent.children;// console.log(childs);var firstChild = parent.firstChild;// console.log(firstChild);var firstElement = parent.firstElementChild;// console.log(firstElement);var lastChild = parent.lastChild;console.log(lastChild);var lastElement = parent.lastElementChild;console.log(lastElement);</script></body>
前面/后面一个
<body><div>one</div><div id="md">middle</div><div>two</div><script>var md = document.getElementById("md");var previousSibling = md.previousSibling;var sibling = md.previousElementSibling;console.log(previousSibling);console.log(sibling);var nextSibling = md.nextSibling;var nextElementSibling = md.nextElementSibling;console.log(nextSibling);console.log(nextElementSibling );</script>
3 增加节点
3-1 appendChild 追加节点(末尾添加节点)
tips:只能添加节点
createElement() 创建节点createTextNode() 创建文本节点
<body><div id="app"><p>one</p></div><script>var app = document.getElementById("app");var p = document.createElement("p");p.innerHTML = "hello world";app.appendChild(p);</script></body>
3-2append
tips:可以添加节点和字符串
<body><div id="app"></div><script>var app = document.getElementById("app");var txt = document.createTextNode("holle world");app.append(txt)</script></body>
3-3prepend 前面加节点
<body><div id="app"><div>first</div></div><script>var app = document.getElementById("app");var p = document.createElement("p");p.innerHTML ="前面";app.prepend(p);</script></body>
4 删除节点
4-1 removeChild
从父节点上删除某个子节点
<body><div id="app"><p>one</p><p id="two">two</p><button id="btn">删除</button></div><script>var app = document.getElementById("app");var two = document.getElementById("two");var btn = document.getElementById("btn");btn.onclick = function(){app.removeChild(two);}</script></body>
4-2remove
<script>var app = document.getElementById("app");var two = document.getElementById("two");var btn = document.getElementById("btn");btn.onclick = function(){two.remove();}</script>
5 替换节点
5-1 replaceChild 替换节点
<body><div id="app"><p id="child">hello world</p></div><script>var app = document.getElementById("app");var h1 = document.createElement("h1");h1.innerHTML = "change";var child = document.getElementById("child");app.replaceChild(h1,child)</script></body>
