How do I loop through children objects in javascript?

前端 未结 6 1829
梦如初夏
梦如初夏 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:22

    I’m surprised no-one answered with this code:

    for(var child=elt.firstChild;
        child;
        child=child.nextSibling){
      do_thing(child);
    }
    

    Or, if you only want children which are elements, this code:

    for(var child=elt.firstElementChild;
        child;
        child=child.nextElementSibling){
      do_thing(child);
    }
    

提交回复
热议问题