使用toggleclass在3个类之间切换 [英] Toggle between 3 classes using toggleclass

查看:836
本文介绍了使用toggleclass在3个类之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张图片需要切换。
我把他们中的每一个都放在课堂上,改变它的图像在CSS中。
我现在可以在两个类之间切换,但是当我添加第三个时,代码不起作用。
他们中的任何一类都可以成为首发。


$ b $ pre $ $('。one')。click(function(){
$(this).toggleClass(两个);
$(this).toggleClass(one);

});点击(函数(){
$(this).toggleClass(one);
$(this).toggleClass(two);

});

当我添加以下行时:

  $(this).toggleClass(three); 

完全不切换....



你知道一种方法,我可以把它们放在一个函数中吗?

谢谢。

解决方案

示例小提琴: http://jsfiddle.net/KdfEV/



这段代码可以让你遍历一系列已定义的类

  $('。one,.two,.three')。click(function(){
this.className = {
three :'one',one:'two',two:'three'
} [this.className];
});


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 third one the code doesn't work. Any class of them can be the starting one.

    $('.one').click(function(){                             
        $(this).toggleClass("two");
        $(this).toggleClass("one");

    });
     $('.two').click(function(){                             
        $(this).toggleClass("one");
        $(this).toggleClass("two");

    });

When I add this line:

        $(this).toggleClass("three"); 

It doesn't toggle at all....

Do you know a method in which I can put all of them in one function ?

Thanks.

解决方案

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];
});

这篇关于使用toggleclass在3个类之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆