Why does document.querySelectorAll return a StaticNodeList rather than a real Array?

前端 未结 6 820
北恋
北恋 2020-11-30 01:11

It bugs me that I can\'t just do document.querySelectorAll(...).map(...) even in Firefox 3.6, and I still can\'t find an answer, so I thought I\'d cross-post on

6条回答
  •  忘掉有多难
    2020-11-30 01:22

    You can use ES2015 (ES6) spread operator:

    [...document.querySelectorAll('div')]

    will convert StaticNodeList to Array of items.

    Here is an example on how to use it.

    [...document.querySelectorAll('div')].map(x => console.log(x.innerHTML))
    Text 1
    Text 2

提交回复
热议问题