Select Element By CSS style (all with given style)

前端 未结 4 1218
无人共我
无人共我 2020-12-09 13:44

Is there a way to select all elements that have a given style using JavaScript?

Eg, I want all absolutely positioned elements on a page.


I would assume

4条回答
  •  心在旅途
    2020-12-09 14:13

    You can keep Mootools, or whatever you use... :)

    function getStyle(el, prop) {
      var view = document.defaultView;
      if (view && view.getComputedStyle) {
        return view.getComputedStyle(el, null)[prop];
      }
      return el.currentStyle[prop];
    }
    
    ​function getElementByStyle(style, value, tag)​ {
      var all = document.getElementsByTagName(tag || "*");
      var len = all.length;
      var result = [];
      for ( var i = 0; i < len; i++ ) {
        if ( getStyle(all[i], style) === value )
          result.push(all[i]);
      }
      return result;
    }
    

提交回复
热议问题