JavaScript increasing variable

前端 未结 5 1224
南方客
南方客 2021-01-18 11:37

I want to add ID to each element of class .content, and I want each ID to have integer increase by 1. Example:

5条回答
  •  -上瘾入骨i
    2021-01-18 11:48

    Use this operator

        var number = 1;
    $(".content").each(function() { 
        $('.content').attr('id', 'content_' + number);
        number++;
    });
    

    or

    var length = $(".content").length;
    
    for(i=1;i

提交回复
热议问题