Node 和 Element
- DOM 是一棵树,所有节点都是 Node
- Node 是 Element 的基类
- Element 是其他 HTML 元素的基类,如 HTMLDivElement
HTMLCollection 和 NodeList
- HTMLCollection 是 Element 的集合
- NodeList 是 Node 的集合
<body>
<p id="p1"><b>node</b>vs<em>element</em><!--这是注释--></p>
</body>
<script>
const p1 = document.getElementById('p1')
console.log(p1.children) // HTMLCollection(2) [b, em]
console.log(p1.childNodes) // NodeList(4) [b, text, em, comment]
</script>
注意
- 获取 Node 和 Element 的返回结果可能不一样
- 如 elem.childNodes 和 elem.children 不一样