开发者

How do I replace special class with another in jquery?

E.g:

<li class="dataItem ready igto" status="ready">I will change status to waiting</li>

and then ,I just want to change the开发者_如何学Python ready class to waiting.THe normal way is to removing the class ready and adding new class!

and another situation is how to replace multi class at once!? E.g:

<!-- replace done wainting error to ready -->
<li class="dataItem done igto" status="ready">I will change status to ready</li>
<li class="dataItem waiting igto" status="ready">I will change status to ready</li>
<li class="dataItem error igto" status="ready">I will change status to ready</li>

to :

<li class="dataItem ready igto" status="ready">I will change status to waiting</li>

Thank you very much!!


You can use $.toggleClass() to achieve this.

$("p").click(function(){
  // Replace 'done' with 'waiting' or 'waiting' with 'done'
  $(this).toggleClass("done waiting");   
});

Example: http://jsfiddle.net/wH9x3/


The best way is to do as you say, remove the class then add it again, as there's no "switching" classes in jQuery.

$('.dataItem').removeClass('ready').addClass('waiting');

You can replace multiple classes by passing in more than one class into .removeClass():

$('.dataItem').removeClass('done waiting error').addClass('ready');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜