在类中添加一个数字,并在每次单击时使用jQuery进行更新

在类中添加一个数字,并在每次单击时使用jQuery进行更新,jquery,html,Jquery,Html,我有一个应用了类的简单锚元素。每次单击锚点时,我都想使用jQuery向类添加一个数字,每次额外的单击都应该将这个数字增加1 默认示例: 第一次点击: 第二次点击: 等等 提前感谢您提供的任何提示或建议试试这个 var clickCount = 0; $("#idname").click(function(event) { $(this).removeClass(clickCount > 0 ? "classname" + clickCount : "className");

我有一个应用了类的简单锚元素。每次单击锚点时,我都想使用jQuery向类添加一个数字,每次额外的单击都应该将这个数字增加1

默认示例:

第一次点击:

第二次点击:

等等

提前感谢您提供的任何提示或建议

试试这个

var clickCount = 0;
$("#idname").click(function(event) {
    $(this).removeClass(clickCount > 0 ? "classname" + clickCount : "className");
    clickCount++;
    $(this).addClass("classname" + clickCount); 
});
var className = "classname", count = 1;
$("a.classname").click(function(){
  this.className = (className + (++count));
});
    $(document).ready(function() {
        className = $('#idname').attr('class');
        $('#idname').click(function() {
            $this = $(this);
            var counter = $this.attr('class').split(className)[1];              
            var nextNum = 1;
            if (counter != '') //if the counter has already started
                nextNum = parseInt(counter) + 1;
            $this.removeClass().addClass(className + nextNum);
        });
    });

这应该可以做到:

var count = 0;
$("#idname").click(function() {
    count++;
    $("#idname").attr('class', 'classname' + count) ;
});
试试这个

var className = "classname", count = 1;
$("a.classname").click(function(){
  this.className = (className + (++count));
});
    $(document).ready(function() {
        className = $('#idname').attr('class');
        $('#idname').click(function() {
            $this = $(this);
            var counter = $this.attr('class').split(className)[1];              
            var nextNum = 1;
            if (counter != '') //if the counter has already started
                nextNum = parseInt(counter) + 1;
            $this.removeClass().addClass(className + nextNum);
        });
    });

完美的非常感谢你的帮助!!我在这里有点冒险,但如果你用JavaScript而不是CSS操纵类名,你可以使用HTML5有效的数据属性;类似于:$'idname'.clickfunction{$this.attr'data-clicked',$this.attr'data-clicked'+1;};
    $(document).ready(function() {
        className = $('#idname').attr('class');
        $('#idname').click(function() {
            $this = $(this);
            var counter = $this.attr('class').split(className)[1];              
            var nextNum = 1;
            if (counter != '') //if the counter has already started
                nextNum = parseInt(counter) + 1;
            $this.removeClass().addClass(className + nextNum);
        });
    });