gpt4 book ai didi

javascript - 在 JavaScript 中,将 NodeList 转换为数组的最佳方法是什么?

转载 作者:IT王子 更新时间:2023-10-29 02:50:42 26 4
gpt4 key购买 nike

DOM 方法 document.querySelectorAll()(以及其他一些方法)返回一个 NodeList

对列表进行操作,例如使用 forEach(),必须首先将 NodeList 转换为 Array

NodeList 转换为 Array 的最佳方法是什么?

最佳答案

使用 ES6 你可以简单地做:

const spanList = [...document.querySelectorAll("span")];

关于javascript - 在 JavaScript 中,将 NodeList 转换为数组的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com