How to increment div id value?

后端 未结 3 524
有刺的猬
有刺的猬 2020-12-16 04:41

I have got a task to set the menu as selected. For that I use dynamic id. So I want to increment it with respect to the selection

My code is

相关标签:
3条回答
  • 2020-12-16 05:23

    Just use a class name instead of an id, you will be able to reuse the code you have just added but in this way.

     var list = $(".menu");
    

    One advice, don't do the highlighting of the item menu with javascript, do it server-side, and you can add the "activating" class directly in the HTML of the LI.

    0 讨论(0)
  • 2020-12-16 05:26

    This is the way to do it with Jquery

    val elementList = $(".menu");
    for (var i = 1; i <= list.length; i++) {
        elementList[i].attr("id", "menu" + i);
    }
    
    0 讨论(0)
  • 2020-12-16 05:37
    var i=0;
    $('.menuHeader').each(function(){
        i++;
        var newID='menu'+i;
        $(this).attr('id',newID);
        $(this).val(i);
    });
    
    0 讨论(0)
提交回复
热议问题