使用jQuery选择多个类 [英] Selecting multiple classes with jQuery
问题描述
我看起来很好看,似乎无法找到如何在一个jQuery选择器语句中选择匹配某些类的所有元素,如下所示:
I’ve had a good look and can’t seem to find out how to select all elements matching certain classes in one jQuery selector statement such as this:
$('.myClass', '.myOtherClass').removeClass('theclass');
有关如何实现这一目标的任何想法?唯一的另一种选择是
Any ideas on how to achieve this? The only other option is to do
$('.myClass').removeClass('theclass');
$('.myOtherClass').removeClass('theclass');
但是我正在使用相当多的类来做这个,所以它需要很多代码。
But I’m doing this with quite a few classes, so it requires much code.
推荐答案
这应该有效:
$(' .myClass,.myOtherClass')。removeClass('theclass');
您必须在第一个参数中将多个选择器全部添加到$ (),否则你给jQuery一个搜索的上下文,这不是你想要的。
You must add the multiple selectors all in the first argument to $(), otherwise you are giving jQuery a context in which to search, which is not what you want.
它和你在CSS中做的一样。
It's the same as you would do in CSS.
这篇关于使用jQuery选择多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!