jQuery - 如何选择所有元素与设置font-family应用于他们? [英] jQuery - How to select all elements with a set font-family applied to them?
本文介绍了jQuery - 如何选择所有元素与设置font-family应用于他们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个jQuery方法来返回所有有css的元素
font-family:AvantGardeITCbyBT-Bold,sans-serif;
。
我认为这样做的唯一方法是循环遍历所有元素,并检查这个css是否应用到它。看起来很慢的做法?
有没有办法通过jQuery选择器这样做?
$(document).ready()可以扩展jQuery选择器, 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');
}
});
});
然后调用
$(':AvantGardel')。hide();
或
$(':SansSerif')。hide();
例如。
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?
解决方案
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
这篇关于jQuery - 如何选择所有元素与设置font-family应用于他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文