jQuery 使用 AND 和 OR 运算符按属性选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10687131/
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 select by attribute using AND and OR operators
提问by The Bndr
I'm thinking about, if it is possible in jQuery to select elements by named attributes using AND and OR.
我在想,是否可以在 jQuery 中使用 AND 和 OR 通过命名属性选择元素。
Example:
例子:
<div myid="1" myc="blue">1</div>
<div myid="2" myc="blue">2</div>
<div myid="3" myc="blue">3</div>
<div myid="4">4</div>
I'd like to select all the elements where myc="blue"
but only those with myid
set to either 1 or 3.
我想选择所有元素,myc="blue"
但只选择那些myid
设置为 1 或 3的元素。
So I tried:
所以我试过:
a=$('[myc="blue"] [myid="1"] [myid="3"]');
but it does not work, same here:
但它不起作用,在这里相同:
a=$('[myc="blue"] && [myid="1"] || [myid="3"]');
Is it possible without writing special filter functions?
是否可以不编写特殊的过滤器功能?
回答by Gabe
ANDoperation
与运算
a=$('[myc="blue"][myid="1"][myid="3"]');
ORoperation, use commas
OR运算,使用逗号
a=$('[myc="blue"],[myid="1"],[myid="3"]');
As @Vega commented:
正如@Vega 评论的那样:
a=$('[myc="blue"][myid="1"],[myc="blue"][myid="3"]');
回答by Felix Kling
Simple use .filter()
[docs](AND) using the multiple selector[docs](OR):
使用多重选择器[docs](OR)简单使用.filter()
[docs](AND ):
$('[myc="blue"]').filter('[myid="1"],[myid="2"]');
In general, chaining selectors, like a.foo.bar[attr=value]
is some kind of AND selector.
一般来说,链接选择器,就像a.foo.bar[attr=value]
某种 AND 选择器。
jQuery has extensive documentationabout the supported selectors, it's worth a read.
jQuery 有关于支持的选择器的大量文档,值得一读。
回答by Selvakumar Arumugam
How about writing a filter like below,
写一个像下面这样的过滤器怎么样,
$('[myc="blue"]').filter(function () {
return (this.id == '1' || this.id == '3');
});
Edit:@Hyman Thanks.. totally missed it..
编辑:@Hyman 谢谢.. 完全错过了..
$('[myc="blue"]').filter(function() {
var myId = $(this).attr('myid');
return (myId == '1' || myId == '3');
});
回答by Ja?ck
First find the condition that occurs in all situations, then filter the special conditions:
首先找出所有情况下出现的条件,然后过滤特殊条件:
$('[myc="blue"]')
.filter('[myid="1"],[myid="3"]');
回答by Guffa
The and operator in a selector is just an empty string, and the or operator is the comma.
选择器中的 and 运算符只是一个空字符串,而 or 运算符是逗号。
There is however no grouping or priority, so you have to repeat one of the conditions:
但是没有分组或优先级,因此您必须重复以下条件之一:
a=$('[myc=blue][myid="1"],[myc=blue][myid="3"]');
回答by Paul
In your special case it would be
在您的特殊情况下,它将是
a=$('[myc="blue"][myid="1"],[myc="blue"][myid="3"]');
回答by philnash
JQuery uses CSS selectors to select elements, so you just need to use more than one rule by separating them with commas, as so:
JQuery 使用 CSS 选择器来选择元素,因此您只需要使用多个规则,用逗号分隔它们,如下所示:
a=$('[myc="blue"], [myid="1"], [myid="3"]');
Edit:
编辑:
Sorry, you wanted blue and 1 or 3. How about:
抱歉,你想要蓝色和 1 或 3。怎么样:
a=$('[myc="blue"][myid="1"], [myid="3"]');
Putting the two attribute selectors together gives you AND, using a comma gives you OR.
将两个属性选择器放在一起为您提供 AND,使用逗号为您提供 OR。
回答by mVChr
To properly select the elements using the logical operations that you've stated, you just need jQuery.filter()
for the AND
operation, not "special filter functions". You also need jQuery.add()
for the OR
operation.
要正确选择使用逻辑操作,你已经规定的元素,你只需要jQuery.filter()
在AND
操作,而不是“特殊的过滤功能”。您还需要jQuery.add()
进行OR
操作。
var elements = $('[myc="blue"]').filter('[myid="1"').add('[myid="3"');
Alternatively, it is possible to accomplish using shorthand in a single selector, where jamming selectors together acts as an AND
and separating with a comma acts as an OR
:
或者,可以在单个选择器中使用速记来完成,其中干扰选择器一起充当 anAND
并用逗号分隔充当OR
:
var elements = $('[myc="blue"][myid="1"], [myid="3"]');