Closures in a for loop

后端 未结 5 1979
温柔的废话
温柔的废话 2020-11-27 07:05

Closures in a loop are causing me problems. I think I have to make another function that returns a function to solve the problem, but I can\'t get it to work with my jQuery

5条回答
  •  鱼传尺愫
    2020-11-27 07:42

    Use the .each function from jquery - I guess you a looping through similar elements - so add the click using something like:

    $(element).children(class).each(function(i){
       $(this).click(function(){
          foo(i);
       });
    });
    

    Not tested but I always use this kind structure where possible.

提交回复
热议问题