Selecting multiple classes with jQuery

2019-01-04 07:05发布

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.

3条回答
姐就是有狂的资本
2楼-- · 2019-01-04 07:39

Have you tried this?

$('.myClass, .myOtherClass').removeClass('theclass');
查看更多
别忘想泡老子
3楼-- · 2019-01-04 07:46

I use $('.myClass.myOtherClass').removeClass('theclass');

查看更多
家丑人穷心不美
4楼-- · 2019-01-04 08:01

This should work:

$('.myClass, .myOtherClass').removeClass('theclass');

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.

It's the same as you would do in CSS.

查看更多
登录 后发表回答