Toggle between 3 classes using toggleclass

后端 未结 3 1623
说谎
说谎 2020-12-18 10:52

I have 3 images that I want to switch between. I put each one of them in a class & change it\'s image in the css. I can now switch between 2 classes but when I add the

相关标签:
3条回答
  • 2020-12-18 11:05
    $('.one, .two, .three').click(function(){                             
            $(this).toggleClass("one, two, three");
        });
    
    0 讨论(0)
  • 2020-12-18 11:11

    The safest way would be to remove unwanted classes and add the required class:

    // Switch to class 'three'
    $(this).removeClass('one').removeClass('two').addClass('three');
    
    0 讨论(0)
  • 2020-12-18 11:15

    Example fiddle: http://jsfiddle.net/KdfEV/

    This code will allow you to cycle through a sequence of defined classes

    $('.one, .two, .three').click(function() {                             
        this.className = {
           three : 'one', one: 'two', two: 'three'
        }[this.className];
    });
    
    0 讨论(0)
提交回复
热议问题