Closures in a for loop

后端 未结 5 1972
温柔的废话
温柔的废话 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:47

    See the bind method.

    $('#button'+i).bind('click', {button: i}, function(event) {
      foo(event.data.button);
    });
    

    From the docs:

    The optional eventData parameter is not commonly used. When provided, this argument allows us to pass additional information to the handler. One handy use of this parameter is to work around issues caused by closures

提交回复
热议问题