var el = document.getElementById('div1').firstChild;

while (el !== null) {
  console.log(el.nodeName);
  el = el.nextSibling;
}

原理: 使用了 Node.prototype.nextSibling 在获取不到后面的同级节点时返回 null 的这个特性.

 

另一个方法是使用 Node.prototype.childNodes.

相关文章:

  • 2021-06-19
  • 2020-04-11
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
  • 2022-01-30
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-03
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案