forEach is not a function error with JavaScript array

前端 未结 11 581
梦毁少年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:13

    parent.children is a HTMLCollection which is array-like object. First, you have to convert it to a real Array to use Array.prototype methods.

    const parent = this.el.parentElement
    console.log(parent.children)
    [].slice.call(parent.children).forEach(child => {
      console.log(child)
    })
    

提交回复
热议问题