How do I loop through children objects in javascript?

前端 未结 6 1815
梦如初夏
梦如初夏 2020-12-24 10:36

I have this code in a function:

tableFields = tableFields.children;
for (item in tableFields) {
    // Do stuff
}

According to a console.lo

6条回答
  •  失恋的感觉
    2020-12-24 11:27

    The backwards compatible version (IE9+) is

    var parent = document.querySelector(selector);
    Array.prototype.forEach.call(parent.children, function(child, index){
      // Do stuff
    });
    

    The es6 way is

    const parent = document.querySelector(selector);
    Array.from(parent.children).forEach((child, index) => {
      // Do stuff
    });
    

提交回复
热议问题