Sam*_*Sam 9 javascript iteration nodelist typescript htmlcollection
将我的JS转换为TS严格模式(非常头痛哈哈)
下面的语法看起来好像没什么问题,但TS在抱怨for环路上allSubMenus有:
[ts] Type 'NodeListOf<Element>' is not an array type or a string type.
我想念什么
function subAct(target:Node){
const allSubMenus : NodeListOf<Element> = document.querySelectorAll('.subMenuItems')
for (const sub of allSubMenus){
sub.classList.remove('active')
}
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ohn 16
这是打字稿侧解析错误。我对HTMLCollectionOf有同样的问题
使任何,它为我工作
const allSubMenus : NodeListOf<Element> = document.querySelectorAll('.subMenuItems')
for (const sub of allSubMenus as any){ // then will pass compiler
sub.classList.remove('active')
}
Run Code Online (Sandbox Code Playgroud)
小智 6
你可以试试
const allSubMenus : NodeListOf<Element> = document.querySelectorAll('.subMenuItems')
Array.from(allSubMenus, subMenu => {/* */})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3820 次 |
| 最近记录: |