A function inside a for loop with jQuery and Javascript
问题 i have the following code : $(document).ready(function () { for (i = 1; i <= number_of_banners; i++) { var selector = "#link_" + i; $(selector).click(function () { alert(i); }); } }); but the alert() can't get the "i" variable from the for loop. How can I use the i variable of for loop inside the .click function ? 回答1: you can use this code : $(document).ready(function () { for (var i = 1; i <= number_of_banners; i++) { var selector = "#link_" + i; $(selector).on('click', {id: i}, function (e