jQuery:过滤多个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15102519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: filter with multiple classes
提问by Adam Cook
Is there a nice way to accomplish filtering selected elements down to a few classes? I know I could do them one at a time, but this just seemed to be something jQuery would allow.
有没有一种很好的方法可以将选定的元素过滤到几个类?我知道我可以一次做一个,但这似乎是 jQuery 允许的。
These are being served up with ajax, and I don't have access to define the actual html.
这些是通过 ajax 提供的,我无权定义实际的 html。
$('.val>td').each(function () {
$(this).filter('.price,.quantity,.remove').children().children().addClass('hidetaxfields');
});
回答by prodigitalson
What you are asking isnt clear from the exmple you give...
从你给出的例子中你并不清楚你在问什么......
This will produce a subset of the elements matched by the inital selector that have the class one
OR two
:
这将生成与具有类one
OR的初始选择器匹配的元素的子集two
:
$(selector).filter('.one, .two');
This will produce a subset of the elements matched by the inital selector that have BOTH the classes one
AND two
:
这将生成与具有两个类one
AND的初始选择器匹配的元素的子集two
:
$(selector).filter('.one.two');
回答by Bryan Downing
Using the .is()
method should work:
使用该.is()
方法应该有效:
$('.val>td').each(function () {
var $this = $(this);
if( $this.is('.price, .quantity, .remove') ){
$this.children().children().addClass('hidetaxfields');
}
});
But this is even better:
但这更好:
$('.val>td.price, .val>td.quantity, .val>td.remove').each(function () {
$(this).children().children().addClass('hidetaxfields');
});
or this:
或这个:
var $tds = $('.val>td').filter('.price, .quantity, .remove');
$tds.each(function () {
$(this).children().children().addClass('hidetaxfields');
});