您的当前位置:首页正文

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

来源:花图问答

本文教程操作环境: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学习推荐:。