在Javascript中,将NodeList转换为数组的最佳方式是什么? [英] In Javascript, what is the best way to convert a NodeList to an array
本文介绍了在Javascript中,将NodeList转换为数组的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DOM方法 document.querySelectorAll()
(和其他几个)返回一个 NodeList
。
要在列表中操作,例如使用 forEach()
, NodeList
必须首先转换为 Array
。
将 NodeList
转换为数组
?
解决方案
使用ES6,您可以简单地:
const spanList = [... document.querySelectorAll(span)];
The DOM method document.querySelectorAll()
(and a few others) return a NodeList
.
To operate on the list, e.g. using forEach()
, the NodeList
must first be converted to an Array
.
What's the best way to convert the NodeList
to an Array
?
解决方案
With ES6 you can simply do:
const spanList = [...document.querySelectorAll("span")];
这篇关于在Javascript中,将NodeList转换为数组的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文