[英]Can I use 'not' and 'contains' filter together in jQuery
I want to apply 'not' and 'contains' filter together to filter element in HTML. 我想将“不”和“包含”过滤器一起应用于HTML中的过滤器元素。
consider I have element as follows , 考虑我有以下元素,
<tr class="filter" value="value1" name="something1">
<tr class="filter" value="value1" name="something2">
<tr class="filter" value="value3" name="something3">
<tr class="filter" value="value1" name="something4">
and I want to apply filter as 我想将过滤器应用为
$(".filter").not("[value*='value1']").contains('something4')
But it does not work. 但这行不通。 Please help me, Thanks.
请帮助我,谢谢。
I would suggest you to use .filter()
method instead: 我建议您改用
.filter()
方法:
var filtered = $(".filter").filter(function(){
return $(this).attr('value') !== 'value1' && $(this).attr('name') === 'something4'
});
filtered.css('color', 'red');
with attribute selectors (Not recommended) : 具有属性选择器(不推荐) :
$(".filter:not([value*='value1']):contains('something4')")
As per your comment: 根据您的评论:
4'th one is my target 第4个是我的目标
Then: 然后:
You don't have to check for negative values but you should go for the equality: 您不必检查负值,但应确保相等:
var filtered = $(".filter").filter(function(){
return $(this).attr('value') === 'value1' && $(this).attr('name') === 'something4'
});
filtered.css('color', 'red');
or: 要么:
$(".filter[value='value1'][name='something4']").css('color', 'red');
You can do this in a single selector, however performance will be better using filter()
as noted in Jai`s answer. 您可以在单个选择器中执行此操作,但是使用Jai的答案中提到的
filter()
可以提高性能。
$(".filter:not([value*='value1']):contains('something4')")
Also note that given your HTML example, this selector will not match anything as you exclude the element with the something4
text with the :not
. 还要注意,在给定HTML示例的情况下,此选择器将不匹配任何内容,因为您使用
:not
排除了带有something4
文本的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.