js中实现遍历dom元素的方法
宋雪维
2021-03-09 09:57:507511浏览 · 0收藏 · 0评论
本文教程操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。
方法一:使用es6中数组rest方法遍历dom
使用语法
var doms=document.querySelectorAll('div')
具体使用
var arr=[...doms] arr.forEach((val)=>{ console.log(val.innerHTML) })
方法二:使用使用firstElementChild,nextElementSibling遍历dom
var list = document.getElementById('list'); var child = list.firstElementChild; while(child){ console.log( child ); child = child.nextElementSibling; }
以上就是JavaScript中遍历dom的两种方法,大家可以选择其中一个使用哦~更多JavaScript学习推荐:JavaScript教程。
关注公众号,随时随地在线学习