jQuery: Toggling between 3 classes (initially)

后端 未结 8 519
你的背包
你的背包 2020-11-29 09:32

I\'ve seen several posts here on SO but they are too specific in functionality and structure, and what I\'m looking for is something more universal that I or anyone can use

相关标签:
8条回答
  • 2020-11-29 10:28

    You can do this :

    $('.toggle').click(function () {
      var classes = ['class1','class2','class3'];
      $('div').each(function(){
        this.className = classes[($.inArray(this.className, classes)+1)%classes.length];
      });
    });
    

    Demonstration

    0 讨论(0)
  • 2020-11-29 10:29

    Here is another approach:

    if ($(this).hasClass('one')) {
        $(this).removeClass('one').addClass('two');
    } else if ($(this).hasClass('two')) {
        $(this).removeClass('two').addClass('three');
    } else if ($(this).hasClass('three')) {
        $(this).removeClass('three').addClass('one');
    }
    
    0 讨论(0)
提交回复
热议问题