jQuery 选择器:逻辑或

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2396596/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:25:11  来源:igfitidea点击:

jQuery selectors: logical OR

jqueryjquery-selectorscss-selectors

提问by RamboNo5

I can't seem to see the forest for the trees right now (Looked at the api docs already).
It has to do with jQuery selectors: What I'm trying to do is to select all elements that have class subtitleor headsection. Something like this: $('.headsection || .subtitle');

我现在似乎看不到树木的森林(已经看过 api 文档)。
它与 jQuery 选择器有关:我想要做的是选择所有具有 classsubtitleheadsection. 像这样的东西:$('.headsection || .subtitle');

To be more specific: I want to use this selector with the function nextUntil.

更具体地说:我想将此选择器与函数一起使用nextUntil

$content = $some_elements.eq(0).nextUntil('.headsection || subtitle');

As far as I know ||is not available in jQuery's selectors. So what's the best way to accomplish that?

据我所知||在 jQuery 的选择器中不可用。那么实现这一目标的最佳方法是什么?

Thanks for your help!

谢谢你的帮助!

回答by BlackAura

It's the same as in CSS selectors:

它与 CSS 选择器中的相同:

$('.headsection, .subtitle');

回答by Felix Kling

What about: $some_elements.eq(0).nextUntil('.headsection, .subtitle');

关于什么: $some_elements.eq(0).nextUntil('.headsection, .subtitle');

Works for me at least. Read about multiple selectors.

至少对我有用。阅读有关多重选择器的信息

回答by Steve

You don't really need a logical OR as such, $('.headsection, .subtitle')should do the job.

你真的不需要这样的逻辑或,$('.headsection, .subtitle')应该完成这项工作。

回答by Justin Ethier

Just separate them with a comma:

只需用逗号分隔它们:

$content = $some_elements.eq(0).nextUntil('.headsection, subtitle');

回答by iblamefish

jQuery uses the CSS selector syntax, so if you know that, just put the same selectors into jQuery and bob's your metaphorical uncle. jQuery uses the sizzleselector engine which supports virtually all CSS3 selectors :)

jQuery 使用 CSS 选择器语法,所以如果您知道这一点,只需将相同的选择器放入 jQuery 中,bob 就是您的隐喻叔叔。jQuery 使用了支持几乎所有 CSS3 选择器的sizzle选择器引擎 :)

As everyone has already said - the way to do it is this: $content = $some_elements.eq(0).nextUntil('.headsection, subtitle');

正如每个人已经说过的那样 - 这样做的方法是这样的: $content = $some_elements.eq(0).nextUntil('.headsection, subtitle');

回答by Eric

Would the multiple selector work for what you're doing? Api doc is here.

多重选择器适用于您正在做的事情吗? Api 文档在这里。