Jquery Class - Add An Increment To It

对着背影说爱祢 提交于 2019-12-08 06:07:07

问题


I am trying to add a dynamically-generated number at the end of "myclass" in Jquery. So I want it to increment a number at the end like this:

myclass1

myclass2

myclass3

myclass4

I've tried a few times but have no clue what I'm doing. Any help would be great!

  var f=1;for(var g=0;g<d.count;g++)for(var f=1; f<3; f++){e.append("<li class='myclass"+f+"'><a>"+f+"</a></li>");f++}

回答1:


I am not sure what you are trying to do, but here's my take on it. Of course, you can be more specific on the jQuery selector.

var i = 1;
$('li').each(function(){
    $(this).addClass('myclass'+i);
    i++;
});

EDIT

Based on the code provided:

for(var f = 1; f < 5; f++){
    e.append("<li class='myclass"+f+"'><a>"+f+"</a></li>");
    f++;
}


来源:https://stackoverflow.com/questions/12610686/jquery-class-add-an-increment-to-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!