在JavaScript中,最好的方式来节点列表转换为数组 [英] in javascript, best way to convert NodeList to Array
本文介绍了在JavaScript中,最好的方式来节点列表转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DOM方法 document.querySelectorAll()
返回节点列表
。
要在列表中的javascript,例如上运行使用的forEach()
的节点列表
必须转换为阵列
。
To operate on the list in javascript, e.g. using forEach()
, the NodeList
must be converted to an Array
.
什么是转换的最佳方式节点列表
到阵列
?
What's the best way to convert the NodeList
to an Array
?
推荐答案
使用ES6可以使用 Array.from(myNodeList)
。然后用你喜欢的阵法。
With ES6 you can use Array.from(myNodeList)
. Then use your favourite array method.
这篇关于在JavaScript中,最好的方式来节点列表转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文