Respond to a bunch of similar events, but each event has a different parameter, in which case you can use JavaScript to bind the event in the For loop, and here's a good example of how you can refer to the following
Sometimes to respond to a bunch of similar events, but the parameters of each event are different, at first thought quite simple, with a for loop do not get, the result found that, the amount is used the last parameter ...
Check the information on the Internet!!! As a result, the great God said to solve with closure
Code:
The code is as follows:
for (Var i=0;i<10;i++) {
btns[i].onclick= (function (i) {
return function () {alert (i)}
}) (i)
}
Presumably because the JavaScript engine executes the code in the For loop first with Btns[i].onclick=function () {alert (i)},
When the user embarks on the onclick event, JavaScript will look for I, and the result will find I, or 10, after the operation is completed.
But with the closure, I will become a local variable of the function.