javascript jQuery CSS“或”选择器

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

jQuery CSS 'or' selector

javascriptjquerycss-selectors

提问by Erin Drummond

I am trying to select elements of a (certain class || another class) with the same selector. How can I go about doing that?

我正在尝试使用相同的选择器选择(某个类 || 另一个类)的元素。我该怎么做呢?

Currently, I have:

目前,我有:

$(".class1 .class2").each(function(idx, el) {... });

however, that only selects elements that match both classes, not one or the other.

然而,它只选择匹配两个类的元素,而不是一个或另一个。

How can I select elements that match one or both of the classes, with the same selector?

如何使用相同的选择器选择匹配一个或两个类的元素?

回答by Nikita Rybak

回答by lukemh

$(".class1,.class2").each(function(idx, el) {... });

put a comma within the same selector string.

在同一个选择器字符串中放置一个逗号。

http://api.jquery.com/multiple-selector/

http://api.jquery.com/multiple-selector/