jQuery:使用多个类进行过滤

时间:2013-02-27 01:17:34

标签: jquery filter

有没有一种很好的方法可以将选定的元素过滤到几个类?我知道我可以一次做一个,但这似乎是jQuery允许的。

这些是用ajax提供的,我无权定义实际的html。

$('.val>td').each(function () {
    $(this).filter('.price,.quantity,.remove').children().children().addClass('hidetaxfields');
});

3 个答案:

答案 0 :(得分:21)

你提出的问题并不清楚你给出的例子......

这将产生由初始选择器匹配的元素的子集,其具有类onetwo

$(selector).filter('.one, .two');

这将生成由初始选择器匹配的元素的子集,其中包含类onetwo

$(selector).filter('.one.two');

答案 1 :(得分:2)

使用过滤器,您可以编写一个过滤函数,可以这样做(demo):

$('.val>td').filter(function () {
    var that = $(this);
    return that.hasClass('price') || that.hasClass('remove') || that.hasClass('quantity');
}).addClass('hidetaxfields');

答案 2 :(得分:1)

使用.is()方法应该有效:

$('.val>td').each(function () {
    var $this = $(this);
    if( $this.is('.price, .quantity, .remove') ){
        $this.children().children().addClass('hidetaxfields');
    }
});

但这更好:

$('.val>td.price, .val>td.quantity, .val>td.remove').each(function () {
    $(this).children().children().addClass('hidetaxfields');
});

或者这个:

var $tds = $('.val>td').filter('.price, .quantity, .remove');
$tds.each(function () {
    $(this).children().children().addClass('hidetaxfields');
});