Assign click handlers in for loop

前端 未结 6 2125
野的像风
野的像风 2020-11-22 03:21

I\'m having several div\'s #mydiv1, #mydiv2, #mydiv3, ... and want to assign click handlers to them:

$(document).ready         


        
6条回答
  •  悲&欢浪女
    2020-11-22 03:26

    $(document).ready(function(){
      for(var i = 0; i < 5; i++) {
       var $li= $('
  • ' + i +'
  • '); (function(i) { $li.click( function(){ alert('you clicked ' + i); }); }(i)); $('#ul').append($li); } });

提交回复
热议问题