使用jQuery添加和删除类

Eva*_*nss -5 jquery

如何点击添加和删除jQuery类?我需要.other-class不受影响.

因此,当您单击.other-class.one时,它将变为.other-class.two.如果单击.other-class.two,它将变为.other-class.one.

这是我到目前为止所做的第二次JS负载工作.

<p class="other-class one">Trigger</p>
Run Code Online (Sandbox Code Playgroud)
.one {
  background: red;
}
.two {
  background: blue;
}
.other-class {
    text-decoration: underline;
}
Run Code Online (Sandbox Code Playgroud)
$('.one').click(function(){
    $(this).removeClass('one');
    $(this).addClass('two');
});

$('.two').click(function(){
    $(this).removeClass('two');
    $(this).addClass('one');
});
Run Code Online (Sandbox Code Playgroud)

Mil*_*war 5

您需要使用事件委派:

$('body').on('click','.one',function(){
 $(this).removeClass('one').addClass('two');
});

$('body').on('click','.two',function(){
 $(this).removeClass('two').addClass('one');
});
Run Code Online (Sandbox Code Playgroud)