HTML and JavaScript auto increment number

前端 未结 6 1834
逝去的感伤
逝去的感伤 2020-12-30 17:04

I am new to HTML and JavaScript. I got a problem like this in HTML (This code below only visualize the problem for you to easy to reference.)


             


        
6条回答
  •  一整个雨季
    2020-12-30 17:28

    Edit: seems like the other solution posted would work do (was added while I typed this up).

    You really should be using PHP to do something dynamic like this, which would become trivial with a single for loop.

    However, if you insist on using HTML/Javascript (or perhaps this is a 'static page'...) then what you are asking should be possible.

    You could add a class to each of the elements you want to use, so:

    
        i
        Harry
    
    
        i
        Simon
    
        i
        Maria
    
    
        i
        Victory
    
    

    Then you would have a javascript function that does something like this:

    var list = document.getElementsByClassName("personid");
    for (var i = 1; i <= list.length; i++) {
        list[i].innerHTML = i;
    }
    

提交回复
热议问题