浏览器原理 · 24/51
1. addEventListener 第三个参数 2. addEventListener 与 attachEvent 区别 3. 浏览器兼容性测试与内核 4. 浏览器兼容性问题 5. 浏览器内核与引擎 6. 浏览器图层创建条件 7. 浏览器多进程架构 8. 浏览器渲染机制 9. 浏览器存储方案 10. 浏览器版本检测方法 11. children 与 childNodes 区别 12. 常见浏览器兼容性问题 13. Chrome 页面进程数量 14. 坐标系统对比 15. 多标签页通讯方案 16. 删除 Cookie 17. 自定义事件 18. DOM 事件处理方式演进 19. 元素尺寸属性对比 20. DOM 节点操作 21. DOM 事件机制 22. addEventListener 与 attachEvent 的区别 23. 获取页面所有复选框 24. HTMLCollection 与 NodeList 区别 25. Hybrid 应用开发 26. 强缓存命中机制 27. 浏览器缓存机制 28. 页面编码与资源编码不一致处理 29. jQuery 事件绑定方法对比 30. Input 点击触发的事件顺序 31. JavaScript 浏览器兼容性问题 32. jQuery 多事件绑定实现 33. JSBridge 原理 34. 链接点击后 Hover 失效解决方案 35. 减少重绘和回流的性能优化 36. 移动端 300ms 点击延迟问题 37. 移动端视口配置 38. 移动端点击穿透问题解决 39. 移动端兼容性问题 40. JSBridge 原理与实现 41. 移动端 1px 像素问题解决方案 42. 浏览器渲染流程 43. 页面加载完成事件对比 44. Offset、Scroll、Client 属性对比 45. 同源策略与跨域解决方案 46. Script 标签位置对页面加载的影响 47. Service Worker 与 PWA 48. 存储方案对比:Cookie、Storage、IndexedDB 49. 强缓存默认时间 50. URL 到页面显示的完整过程 51. V8 引擎 JavaScript 执行过程

HTMLCollection 与 NodeList 区别

比较 HTMLCollection 和 NodeList 的获取方式、内容类型和动态特性

问题

HTMLCollection 和 NodeList 有什么区别?

解答

获取方式

// HTMLCollection
const byId = document.getElementsByClassName('item');
const byTag = document.getElementsByTagName('div');
const children = document.body.children;

// NodeList
const byQuery = document.querySelectorAll('.item');  // 静态
const childNodes = document.body.childNodes;          // 动态

包含内容

const container = document.getElementById('container');

// HTMLCollection - 只包含元素节点
console.log(container.children);
// HTMLCollection [div, span, p]

// NodeList - 包含所有节点(元素、文本、注释等)
console.log(container.childNodes);
// NodeList [text, div, text, span, text, p, text]

动态性差异

<ul id="list">
  <li>1</li>
  <li>2</li>
</ul>

<script>
const list = document.getElementById('list');

// HTMLCollection - 始终是动态的
const htmlCollection = list.getElementsByTagName('li');

// NodeList - querySelectorAll 返回静态,childNodes 返回动态
const staticNodeList = list.querySelectorAll('li');
const liveNodeList = list.childNodes;

console.log(htmlCollection.length); // 2
console.log(staticNodeList.length); // 2

// 添加新元素
const newLi = document.createElement('li');
newLi.textContent = '3';
list.appendChild(newLi);

console.log(htmlCollection.length);  // 3 - 自动更新
console.log(staticNodeList.length);  // 2 - 保持不变
</script>

可用方法

const nodeList = document.querySelectorAll('div');
const htmlCollection = document.getElementsByTagName('div');

// NodeList 支持 forEach
nodeList.forEach(node => console.log(node));

// HTMLCollection 不支持 forEach,需要转换
// 方法1:Array.from
Array.from(htmlCollection).forEach(el => console.log(el));

// 方法2:展开运算符
[...htmlCollection].forEach(el => console.log(el));

// 方法3:借用数组方法
Array.prototype.forEach.call(htmlCollection, el => console.log(el));

访问方式

// 两者都支持索引访问和 item() 方法
nodeList[0];
nodeList.item(0);

htmlCollection[0];
htmlCollection.item(0);

// HTMLCollection 额外支持 namedItem(),通过 id 或 name 访问
// <div id="myDiv"></div>
htmlCollection.namedItem('myDiv');
htmlCollection['myDiv'];  // 简写形式

关键点

  • 内容类型:HTMLCollection 只含元素节点,NodeList 可含所有节点类型
  • 动态性:HTMLCollection 始终动态;NodeList 中 querySelectorAll 返回静态,childNodes 返回动态
  • 遍历方法:NodeList 有 forEach,HTMLCollection 需转为数组才能用
  • 获取方式getElementsBy* 返回 HTMLCollection,querySelectorAll 返回 NodeList
  • 命名访问:HTMLCollection 支持 namedItem() 按 id/name 访问元素