jQuery - How to select all elements with a set font-family applied to them?

我们两清 提交于 2019-12-30 06:49:57

问题


I need a way for jQuery to return all elements that have the css

font-family:AvantGardeITCbyBT-Bold, sans-serif;

applied to them.

I'm thinking the only way of doing this is looping through all elements and checking if this css is applied to it. Seems a slow way of doing it?

Is there a way of doing this via a jQuery Selector?


回答1:


well, you can extend the jQuery selectors with

$(document).ready(function(){
   $.extend($.expr[':'], {
     AvantGardel: function(elem){
        var $e = $(elem);
        return( typeof $e.css('font-family') !== 'undefined' && $e.css('font-family') === 'AvantGardeITCbyBT-Bold' );
     },
     SansSerif: function(elem){
       var $e = $(elem);
        return( typeof $e.css('font-family') !== 'undefined' && $e.css('font-family') === 'sans-serif' );
     }
 });    
});

and then call

$(':AvantGardel').hide();

or

$(':SansSerif').hide();

for instance.

working example: http://jsbin.com/idova3/edit



来源:https://stackoverflow.com/questions/3044465/jquery-how-to-select-all-elements-with-a-set-font-family-applied-to-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!