简体   繁体   English

添加课程后,再次单击并添加课程

[英]click and addclass again after class has been added

as the title says I want to add classes to a div without using toggle.' 如标题所示,我想在不使用切换的情况下将类添加到div。”

so basically I want to add a class to a footer, I can achieve this by using: 所以基本上我想将一个类添加到页脚中,我可以使用以下方法实现此目的:

$('footer').click(function(){
   $('footer').addClass('class-one');
});

but the problem is that I want to add a class again by clicking once more while class-one exist 但问题是我想在存在第一个类的情况下再次单击以再次添加一个类

then again just adding 然后再次添加

$('.class-one').click(function(){
     $('.class-one').addClass('class-two');
     $('.class-two').removeClass('class-one');
});

repeat this

$('.class-two').click(function(){
    $('.class-two).addClass('class-three');
    $('.class-three).removeClass('class-two);

doesn't work. 不起作用。

can someone tell me how I can achieve my goal? 有人可以告诉我如何实现我的目标吗? so i can repeat this like 5~9times? 这样我可以重复5到9次吗?

If my question is hard to understand what Im trying to do is that 如果我的问题很难理解,我想做的是

imagine Im clicking 想象我点击

picture of a ball and I click it so that it turns into triangle 球的图片,我单击它使其变成三角形

when I click triangle it turns into a square 当我单击三角形时,它变成一个正方形

and I click it again and it turns into a hexagon 然后再次单击它,它变成一个六角形

Put your classes in an Array and then use an index to access the class and also make use of a counter which will act as our index in this case, which we will update using increment operator. 将您的类放在Array ,然后使用索引来访问该类,并利用计数器作为本例中的索引,我们将使用增量运算符对其进行更新。

var classes = ["class-one", "class-two"], counter = 0;
$('footer').click(function(){
    $(this).removeClass(classes[counter]).addClass(classes[counter++]);
});

Check this out: 看一下这个:

Eg: 例如:

$('.one').click(function(e)
{
    $(e.currentTarget).removeClass('one');
    $(e.currentTarget).addClass('two');
});

Refer: http://jsfiddle.net/3uz03b5r/ 参考: http//jsfiddle.net/3uz03b5r/

Your Code: 您的代码:

$('.class-one').click(function(e){
     $(e.currentTarget).addClass('class-two');
     $(e.currentTarget).removeClass('class-one');
});

Fiddle Demo 小提琴演示

Try this one 试试这个

Html HTML

<div class = "set0">Test<div>

Jquery jQuery的

var i = 0;
$('.set'+i).on('click',function(){
    $(this).removeClass('set'+i);
    i++;
   $(this).addClass('set'+i);
});

Try this.. 尝试这个..

<div id="facts">
<p>(click me):</p>
<input type="hidden" class="old" value="0"/>
  <div class="class-1"><div>
</div>
<script>
$(document).ready(function() {
    var current = 1;
    $('#facts').click(function() {
     var old= $(".old").val();
      var newvalue= parseInt(current)+parseInt(old); 
$(".old").val(newvalue);      
    $('.class-'+old).addClass('class-'+newvalue);
   $('.class-'+newvalue).removeClass('class-'+old);

    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM