• 技术文章 >Web开发 >JavaScript

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

    宋雪维宋雪维2021-03-09 09:57:50原创6559

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

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

    使用语法

    1

    var doms=document.querySelectorAll('div')

    具体使用

    1

    2

    3

    4

    var arr=[...doms]

    arr.forEach((val)=>{

        console.log(val.innerHTML)

    })

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

    1

    2

    3

    4

    5

    6

    7

    8

    var list = document.getElementById('list');

      

    var child = list.firstElementChild;

      

    while(child){

        console.log( child );

        child = child.nextElementSibling;

    }

    以上就是JavaScript中遍历dom的两种方法,大家可以选择其中一个使用哦~更多JavaScript学习推荐:JavaScript教程

    专题推荐:js 遍历dom
    上一篇:js中bom是什么意思 下一篇:js中如何递归遍历dom树

    相关文章推荐

    • python中标准库struct如何使用?• 如何使用python爬取抖音视频?• python中的插入排序怎么用?• python中elasticsearch是什么?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网