jQuery或选择器?

本文翻译自:jQuery OR Selector?

I am wondering if there is a way to have "OR" logic in jQuery selectors. 我想知道是否有一种方法可以在jQuery选择器中使用“或”逻辑。 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是否提供此类功能?


#1楼

参考:https://stackoom.com/question/9Uxj/jQuery或选择器


#2楼

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'));

#3楼

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. 希望对所有可能通过Google进入此页面的人有帮助。


#4楼

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. 但是,通常,当选择器是一个相当长的字符串时,我会使用数组方法。


#5楼

Use a comma. 使用逗号。

'.classA, .classB'

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


#6楼

Daniel A. White Solution works great for classes. Daniel A. White Solution非常适合上课。

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. 虽然我不确定它的优化程度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值