JavaScript increasing variable

前端 未结 5 1230
南方客
南方客 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 12:08

    You can use the index parameter of the .each function callback instead of your own counter:

    $(".content").each(function(i) {
      $(this).prop('id', 'content_' + (i+1));
    });
    

提交回复
热议问题