節點關系
- children
- childNodes
- parentNode
- firstChild
- lastChild
- proviousSibling
- proviusElementSibling
- nxetSibling
- nextElementSibling
DOM的增刪改查
- 增加,也就是動態創建
- document.crentElement("標簽名")
- cloneNode(Boolean)
- Boolean = true, 深度克隆,也就是把子子孫孫都克隆下來
- Boolean = false,默認就是false,只是克隆表面
- 動態插入
- parent.appendChild(newEle) 把新元素插入到父容器末尾
- parent.insertBefore(newEle,oldEle) 把新元素插入到指定元素的前面
- 動態替換
- parent.replaceChild(newEle,oldEle) 那newEle替換oldELe
- 刪除元素
- parent.removeChild(ele) 移出
dom的屬性操作
- 屬性操作用“.” 和 [];凡是點能操作的,[]都能操作
- []能操作數字,變量,但是不能用"."來操作
- 屬性操作,attribute
- getAttribute(屬性名) 獲取屬性
- setAttribut(屬性名,屬性值) 設置屬性
- removeAttribute(屬性名) 移除屬性
- "." 和 attribute的區別:
- 在‘獲取’的時候:元素標簽上的自定義屬性,通過"."拿到的是undefined;但是通過getAttribute(屬性名) 可以拿到真正的屬性值
- 在“設置”的時候:通過"."設置的自定義屬性,標簽上是看不到的,但是通過setAttribute(屬性名,屬性值) 設置的屬性,在標簽上可以看到