如何切换特定类集 [英] How to toggle a specific class set
本文介绍了如何切换特定类集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列课程:
- classA
- classB
- classC
且我有一个< span class ='fixedClass'>
,需要根据特定条件在这些类之间切换。您可以说使用 jQuery toggleClass
and I have a <span class='fixedClass'>
which needs to be toggled between one of these classes depending on a specific condition. You might say to use jQuery toggleClass
在jQuery中,最快和最短的代码是什么?
What's fastest and shortest code in jQuery to do this?
推荐答案
此示例可能会帮助您: http:// jsfiddle .net / e9bj5 /
This sample may help you: http://jsfiddle.net/e9bj5/
$(function(){
$('.fixedClass').on("click", function() {
var box = $(this);
if ( box.hasClass('red') ) {
box.removeClass('red').addClass('green');
}
else if ( box.hasClass('green') ) {
box.removeClass('green').addClass('blue');
}
else if ( box.hasClass('blue') ) {
box.removeClass('blue').addClass('red');
}
});
});
这篇关于如何切换特定类集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文