Iterating through all the
tags on a page

前端 未结 3 1844
遥遥无期
遥遥无期 2021-02-03 20:10

I want to go through all the elements on a page using Javascript and see if they have a property set. Is there an easy way to do this, or do I have to use a recursive solution?

相关标签:
3条回答
  • 2021-02-03 20:19

    To find a property in one or more of all divs on a page:

    var divs = document.getElementsByTagName("div"), i=divs.length;
    while (i--) {
       if (divs[i].getAttribute([yourProperty]) === 'yourValue'){
          //do something
       } 
    }
    
    0 讨论(0)
  • 2021-02-03 20:22

    You might also be able to use a selector engine such as Sizzle.

    Steve

    0 讨论(0)
  • 2021-02-03 20:26

    You can use:

    var divs = document.getElementsByTagName("div");
    for(var i = 0; i < divs.length; i++){
       //do something to each div like
       divs[i].innerHTML = "something new...";
    }
    
    0 讨论(0)
提交回复
热议问题