使用jQuery切换多个元素类

问题描述:

当我单击一个元素时,我需要能够切换类.对于该元素,它工作正常:

I need to be able to toggle class when I click an element. It works fine for that element:

$(".main").click(function() {
    $(this).toggleClass("classA").toggleClass("classB");
});

但是我想添加它以切换页面上具有特定类的所有元素的类,例如"togToo".对于这些元素,我需要在"classC"和"classD"之间切换.

But I would like to add to it to toggle classes for all elements on the page that have a particular class, say "togToo". For those elements I need to toggle between "classC" and "classD".

我几乎可以肯定,有一种方法可以将其组合为一键...

I'm almost certain there's a way to combine that into one click...

^^^更新于^^^

^^^ UPDATED ABOVE ^^^

答案已更新

$(".main").click(function() {
    $(this).toggleClass("classA").toggleClass("classB");
    $('.togToo').toggleClass("classC").toggleClass("classD");
})