简体   繁体   English

jQuery 或选择器?

[英]jQuery OR Selector?

I am wondering if there is a way to have "OR" logic in jQuery selectors.我想知道是否有办法在 jQuery 选择器中使用“OR”逻辑。 For example, I know an element is either a descendant of an element with class classA or classB, and I want to do something like elem.parents('.classA or .classB') .例如,我知道一个元素是类 classA 或 classB 的元素的后代,并且我想做一些类似elem.parents('.classA or .classB') Does jQuery provide such functionality? jQuery 提供这样的功能吗?

Use a comma.使用逗号。

'.classA, .classB'

You may choose to omit the space.您可以选择省略空格。

Using a comma may not be sufficient if you have multiple jQuery objects that need to be joined.如果您有多个需要连接的 jQuery 对象,使用逗号可能还不够。

The .add() method adds the selected elements to the result set: .add()方法将所选元素添加到结果集中:

// classA OR classB
jQuery('.classA').add('.classB');

It's more verbose than '.classA, .classB' , but lets you build more complex selectors like the following:它比'.classA, .classB'更冗长,但可以让您构建更复杂的选择器,如下所示:

// (classA which has <p> descendant) OR (<div> ancestors of classB)
jQuery('.classA').has('p').add(jQuery('.classB').parents('div'));

I have written an incredibly simple (5 lines of code) plugin for exactly this functionality:我为这个功能编写了一个非常简单的(5 行代码)插件:

http://byrichardpowell.github.com/jquery-or/ http://byrichardpowell.github.com/jquery-or/

It allows you to effectively say "get this element, or if that element doesnt exist, use this element".它允许您有效地说“获取此元素,或者如果该元素不存在,则使用此元素”。 For example:例如:

$( '#doesntExist' ).or( '#exists' );

Whilst the accepted answer provides similar functionality to this, if both selectors (before & after the comma) exist, both selectors will be returned.虽然接受的答案提供了与此类似的功能,但如果两个选择器(逗号前后)都存在,则将返回两个选择器。

I hope it proves helpful to anyone who might land on this page via google.我希望它对可能通过谷歌登陆此页面的任何人都有帮助。

If you're looking to use the standard construct of element = element1 ||如果您希望使用 element = element1 || 的标准构造element2 where JavaScript will return the first one that is truthy, you could do exactly that: element2 JavaScript 将返回第一个真实的元素,你可以这样做:

element = $('#someParentElement .somethingToBeFound') || $('#someParentElement .somethingElseToBeFound');

which would return the first element that is actually found.这将返回实际找到的第一个元素。 But a better way would probably be to use the jQuery selector comma construct (which returns an array of found elements) in this fashion:但更好的方法可能是以这种方式使用 jQuery 选择器逗号构造(它返回一个已找到元素的数组):

element = $('#someParentElement').find('.somethingToBeFound, .somethingElseToBeFound')[0];

which will return the first found element.这将返回第一个找到的元素。

I use that from time to time to find either an active element in a list or some default element if there is no active element.如果没有活动元素,我会不时使用它来查找列表中的活动元素或某个默认元素。 For example:例如:

element = $('ul#someList').find('li.active, li:first')[0] 

which will return any li with a class of active or, should there be none, will just return the last li.这将返回任何具有活动类的 li,或者,如果没有,将只返回最后一个 li。

Either will work.要么会起作用。 There are potential performance penalties, though, as the ||但是,存在潜在的性能损失,因为 || will stop processing as soon as it finds something truthy whereas the array approach will try to find all elements even if it has found one already.一旦发现某些内容,它就会停止处理,而数组方法将尝试找到所有元素,即使它已经找到了一个。 Then again, using the ||然后再次使用 || construct could potentially have performance issues if it has to go through several selectors before finding the one it will return, because it has to call the main jQuery object for each one (I really don't know if this is a performance hit or not, it just seems logical that it could be).如果在找到要返回的选择器之前必须经过多个选择器,则构造可能会出现性能问题,因为它必须为每个选择器调用主 jQuery 对象(我真的不知道这是否会影响性能,它可能是合乎逻辑的)。 In general, though, I use the array approach when the selector is a rather long string. In general, though, I use the array approach when the selector is a rather long string.

Finally I've found hack how to do it:最后我找到了破解方法:

div:not(:not(.classA,.classB)) > span

(selects div with class classA OR classB with direct child span) (选择类classAclassB div 与直接子跨度)

Daniel A. White Solution works great for classes. Daniel A. White 解决方案非常适合课堂。

I've got a situation where I had to find input fields like donee_1_card where 1 is an index.我遇到了一种情况,我必须找到像donee_1_card这样的输入字段,其中 1 是一个索引。

My solution has been我的解决方案是

$("input[name^='donee']" && "input[name*='card']")

Though I am not sure how optimal it is.虽然我不确定它有多优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM