Auto increment numbers in div id javascript

前端 未结 2 1041
南方客
南方客 2021-01-07 15:56

can somebody please help me? How to add auto increment number in div ID using javascript? I have four divs and I\'d like to have them automatically numbered (box1, box2, box

2条回答
  •  隐瞒了意图╮
    2021-01-07 16:28

    set id property

    var list = document.getElementsByClassName("something");
    for (var i = 0; i < list.length; i++) {
        list[i].id = "box" + (i + 1);
    }
    

提交回复
热议问题