js中实现遍历dom元素的方法

本文教程操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

方法一:使用es6中数组rest方法遍历dom

使用语法

vardoms=document.querySelectorAll('div')

具体使用

vararr=[...doms]
arr.forEach((val)=>{
console.log(val.innerHTML)
})

方法二:使用使用firstElementChild,nextElementSibling遍历dom

varlist=document.getElementById('list');

varchild=list.firstElementChild;

while(child){
console.log(child);
child=child.nextElementSibling;
}
原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享