HTMLCollection 和 NodeList 的区别

166 阅读1分钟

Node 和 Element

  • dom 是一棵树,所有节点都是 Node

  • Node 是 element 的基类

  • Element 是其他 HTML 元素的基类,如 HTMLDivElement

image.png

HTMLCollection 和 NodeList

  • HTMLCollection 是 Element 的集合

  • NodeList 是 Node 的集合

  • HTMLCollection 和 NodeList 是类数组

将类数组 -> 数组

const arr1 = Array.from(list);

const arr2 = Array.prototype.slice.call(list);

const arr3 = [...list];