Select Element By CSS style (all with given style)

前端 未结 4 1217
无人共我
无人共我 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:15

    In jQuery you could use

    $('*').filter( function(){
      return ($(this).css('position') == 'absolute');
    } );
    

    [update]

    Or even create a new selector.
    got me interested and so here is one (its my 1st, so its not built for efficiency) to find elements by css property..

    $.expr[':'].css = function(obj, index, meta, stack){
      var params = meta[3].split(',');
    
      return ($(obj).css(params[0]) == params[1]);
    };
    

    usage: $('optionalSelector:css(property,value)')
    will return all elements (of optionalSelector) whose property = value

    example: var visibleDivs = $('div:css(visibility,visible)');
    will return all divs whose visibility is set to visible (works for the default visibility as well..)

提交回复
热议问题