Automatically generating unique DOM ids?

前端 未结 4 2205
别那么骄傲
别那么骄傲 2020-12-18 22:33

When coding with JS and the DOM I find myself constantly needing to generate ids (or names) that have no purpose other than to group DOM elements together (or r

4条回答
  •  眼角桃花
    2020-12-18 22:39

    Example of working with radio labels without ID:



    Show me when 1st radio checked
    Show me when 2nd radio checked
    Show me when 3rdd radio checked

    JS

    $('.button-group[name=sys] :radio').change(function(){
        $(this).parent().find('label').removeClass('active');
         $(this).next().addClass('active');
    
          /* index radios vs content*/
    
          var radioIndex=$('.button-group[name=sys] :radio').index(this);
           /* hide all the content div, show one that matches radio index*/
           $('.content div').hide().eq(radioIndex).show();
    })
    

提交回复
热议问题