Get all visible DIVs on a page with javascript?

后端 未结 2 2040
醉梦人生
醉梦人生 2020-12-21 04:20

Another short Q, is there any short piece of code to get all DIVs on a page which have the visibility set to \'block\' or \'inline\'?

Thank

相关标签:
2条回答
  • 2020-12-21 04:46

    It's easy with jQuery...

    $("div:visible")
    

    But if you want to be old school...

    var divs = document.getElementsByTagName("DIV");
    var elems = [];
    
    for(var i = 0; i < divs.length; i++) {
      var div = divs[i];
      var vis = div.style.visibility;
    
      if(vis == 'block' || vis == 'inline')
        elems.push(div);
    }
    
    0 讨论(0)
  • 2020-12-21 04:59

    Using jQuery:

    $("div:visible")
    

    http://api.jquery.com/visible-selector/

    0 讨论(0)
提交回复
热议问题