楼主: 1192025125
52 0

XML DOM - NodeList 对象 [推广有奖]

  • 0关注
  • 0粉丝

等待验证会员

小学生

42%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
50 点
帖子
4
精华
0
在线时间
0 小时
注册时间
2018-3-2
最后登录
2018-3-2

楼主
1192025125 发表于 2025-11-26 11:26:49 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

XML DOM - NodeList 对象速查指南(2025 更新版)

掌握以下核心内容,足以应对 99% 的实际开发场景与面试考察。无需死记硬背全部 API,聚焦高频用法即可高效上手。

parent.querySelectorAll("book")

最常用获取方式

推荐使用 querySelectorAll() 方法获取 NodeList,返回的是静态节点列表,性能稳定且兼容性良好。

const nodes = root.querySelectorAll("selector");
parent.getElementsByTagName("book")

NodeList 长度属性

所有 NodeList 实例均具备 length 属性,可用于判断节点数量或控制循环流程。

console.log(nodeList.length);
nodeList.length

访问指定位置的节点

通过索引可直接获取 NodeList 中的某一项,支持正向与负向索引(后者需现代浏览器支持)。

  • 获取第 n 个节点:nodeList[n]
  • 获取最后一个节点:nodeList[nodeList.length - 1]nodeList.at(-1)
nodeList[0]

nodeList.at(-1)

遍历 NodeList:for...of 与 forEach

现代开发中推荐优先使用 for...of 和原生 forEach 进行遍历操作。

// 推荐方式一:for...of
for (const node of nodeList) {
  console.log(node);
}

// 推荐方式二:forEach(ES6+ 原生支持)
nodeList.forEach(node => {
  console.log(node);
});
for (const node of nodeList) { … }

nodeList.forEach(node => { … })

将 NodeList 转换为真实数组

当需要调用 mapfilterreduce 等数组方法时,必须先将其转为真正的数组。

[...nodeList]
Array.from(nodeList)
[...nodeList]

Array.from(nodeList)

能否直接使用 .map()?

不能直接调用 .map()。必须先转换成数组后再进行链式操作。

const titles = [...nodeList].map(el => el.textContent);
[...nodeList].map(...)

是否实时更新?关键区别解析

NodeList 是否“活的”取决于其来源:

  • 静态 NodeList(如 querySelectorAll 返回)→ 不会随 DOM 变化自动更新
    querySelectorAll
  • 动态 HTMLCollection(如 getElementsByTagName 返回)→ 实时反映 DOM 状态
    getElementsByTagName

这是两者最核心的区别,务必牢记。

2025 年只需记住这三行代码

在绝大多数项目中,以下模式已足够覆盖全部需求:

// 1. 获取节点列表(推荐写法)
const books = root.querySelectorAll("book");     // 静态 NodeList

// 2. 遍历处理(任选其一)
books.forEach(book => console.log(book));        // 优雅简洁
for (const book of books) { ... }                  // 直观易懂

// 3. 使用 map/filter/reduce:先转数组
const titles = [...books].map(b => b.querySelector("title").textContent);

NodeList vs HTMLCollection 对比一览表

特性 NodeList(querySelectorAll) HTMLCollection(getElementsByTagName)
是否“活的” 否(静态快照) 是(动态同步更新)
原生支持 forEach 是(ES6+ 全面支持) 否(需借用 Array.prototype)
能否直接 .map() 不能 不能
2025 推荐程度 强烈推荐 仅用于需动态集合的特殊场景
典型应用场景 99% 日常开发 老项目维护、动态表单监控

实战代码模板(可直接复用)

// 标准写法(2025 推荐)
const allBooks = root.querySelectorAll("book");

// 输出所有书名
allBooks.forEach(book => {
  console.log(book.querySelector("title")?.textContent);
});

// 筛选出价格高于 50 的书籍
const expensive = [...allBooks].filter(book =>
  parseFloat(book.querySelector("price")?.textContent) > 50
);

// 批量添加 data-index 属性
allBooks.forEach((book, i) => book.setAttribute("data-index", i));

// 获取最后一本书(两种方式)
const lastBook = allBooks[allBooks.length - 1];
const lastBook2 = allBooks.at(-1);   // ES2022 新语法,更便捷

常见误区与解决方案

问题 错误做法 正确解法(2025)
调用 forEach 报错
getElementsByTagName("book").forEach
确保使用 querySelectorAll 获取 NodeList(原生支持 forEach)
filter 操作失败
nodeList.filter(...)
先转数组:[...list].filter(...)
[...nodeList].filter(...)
新增节点后列表未更新 误以为 NodeList 是动态的 正确行为!静态 NodeList 不会自动更新,需重新查询
需要实时监听节点变化 坚持使用 querySelectorAll 改用 getElementsByTagName 等返回动态集合的方法

2025 终极记忆口诀

只需记住这一句话,轻松掌握 NodeList 核心用法:

“获取用 querySelectorAll
querySelectorAll

遍历用 forEach
forEach

想用 map/filter 就加 [...list]
...

熟记本指南中的核心表格、三行标准代码和一句口诀,你对 NodeList 的理解与应用能力已远超大多数开发者,各类批量操作、筛选、遍历均可快速实现。

如果你需要一个「NodeList 实时操作可视化演示页面」,可以随时告诉我。该页面能够直观展示操作过程中 NodeList 长度的动态变化。
parent.querySelectorAll("book")
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Node list del Eli XML

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
扫码
拉您进交流群
GMT+8, 2026-2-2 18:55