JavaScript increasing variable

前端 未结 5 1269
南方客
南方客 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条回答
  •  长发绾君心
    2021-01-18 11:59

    Try this

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

    This is iterate single elements one by one rather than taking all elements with .content

提交回复
热议问题