Javascript - child node count

前端 未结 4 1985
甜味超标
甜味超标 2020-12-14 16:52
  • Array1
  • Array2
  • Array3
4条回答
  •  情深已故
    2020-12-14 17:35

    If you want to get a list of all nested DOM nodes, you can use this code snippet:

    function getNestedElements(node) {
      const elems = [];
      const children = node.childNodes;
    
      for (let i = 0; i < children.length; i++) {
        if (children[i].nodeType === document.ELEMENT_NODE) {
          elems.push(children[i], ...getNestedElements(children[i]));
        }
      }
      return elems;
    }
    

    This is a modified version of @julien-ch answer

提交回复
热议问题