querySelector() where display is not none

前端 未结 3 1217
独厮守ぢ
独厮守ぢ 2021-02-20 11:29

I have a long list of

  • items I need to filter. I want the visible ones. Here\'s an example hidden one:

  • 3条回答
    •  遥遥无期
      2021-02-20 12:12

      • Use '.newSearchResultsList li' selector to select all the li elements
      • Use Array#filter over collection
      • Use getComputedStyle to get all styles associated with element
      • Return only those elements having style !== none

      var liElems = document.querySelectorAll('.newSearchResultsList li');
      var filtered = [].filter.call(liElems, function(el) {
        var style = window.getComputedStyle(el);
        return (style.display !== 'none')
      });
      console.log(filtered);
      • dogscats
      • Visible

    提交回复
    热议问题