JavaScript increasing variable

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

    Try this:

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

    Note: you could just use vanilla JS to assign the attribute id (no need to use jQuery)

提交回复
热议问题