forEach is not a function error with JavaScript array

前端 未结 11 577
梦毁少年i
梦毁少年i 2020-11-29 17:11

I\'m trying to make a simple loop:

const parent = this.el.parentElement
console.log(parent.children)
parent.children.forEach(child => {
  console.log(chil         


        
11条回答
  •  情话喂你
    2020-11-29 18:11

    That's because parent.children is a NodeList, and it doesn't support the .forEach method (as NodeList is an array like structure but not an array), so try to call it by first converting it to array using

    var children = [].slice.call(parent.children);
    children.forEach(yourFunc);
    

提交回复
热议问题