我一直在尝试一种在打字稿上获取以下内容的方法:
[...document.querySelectorAll<HTMLElement>("#node-a input, #node-a button")].forEach(item => {
//code
})
但是我收到以下错误:
Type 'NodeListOf<HTMLElement>' is not an array type.ts(2461)
这有什么问题?
document.querySelectorAll
返回一个NodeList
,而不是一个数组。若要将其视为数组,可以按照建议使用Array.from()
,或者更改代码以改为使用 NodeList。请注意,某些浏览器不支持将forEach()
与 NodeList 一起使用。