Original reference: http://blog.csdn.net/gaoshanwudi/article/details/7355794
Beginners often encounter, that is, get the HTML element collection, loop to add events to the element. The corresponding index is obtained in the event response function (handler). But each fetch is the index of the last loop. The reason is that beginners do not understand the characteristics of JavaScript closures.
A netizen asked a question, such as the next HTML, why click All the paragraph p output is 5, rather than alert out the corresponding 0,1,2,3,4.
1. <! DOCTYPE html>
2.
3.
4. <meta charset= "Utf-8"/>
5. <title> Closed Package Demo </title>
6. <style type= "Text/css" >
7. p {background:gold;}
8. </style>
9. <script type= "Text/javascript" >
10.function Init () {
var pary = document.getElementsByTagName ("P");
for (var i=0; i<pary.length; i++) {
Pary[i].onclick = function () {
alert (i);
15.}
16.}
17.}
18.</script>
19.
20.<body onload= "Init ();" >
21.<p> Product 0</p>
22.<p> Product 1</p>
23.<p> Product 2</p>
24.<p> Product 3</p>
25.<p> Product 4</p>
26.</body>
27.
The above scenes are often encountered by beginners. That is, the collection of HTML elements that loop to add events to the element. The corresponding index is obtained in the event response function (handler). But each fetch is the index of the last loop.
The reason is that beginners do not understand the characteristics of JavaScript closures. Through Element.onclick=function () {alert (i);} method to add a click event to an element. Response functions function () {alert (i);} I is not the corresponding I (such as 0,1,2,3,4) in each cycle, but the value of the last I after the loop is 5. In other words, the response function in the loop is not able to save the corresponding value I, but the last i++ value of 5.
Understand the reasons, the following are several ways to solve:
1. Save the variable i to each paragraph object (p)
1. function init1 () {
2. var pary = document.getElementsByTagName ("P");
3. for (var i=0; i<pary.length; i++) {
4. pary[i].i = i;
5. Pary[i].onclick = function () {
6. Alert (THIS.I);
7.}
8.}
9.}
2. Save the variable i in the anonymous function itself
1. function Init2 () {
2. var pary = document.getElementsByTagName ("P");
3. for (var i=0; i<pary.length; i++) {
4. (Pary[i].onclick = function () {
5. Alert (ARGUMENTS.CALLEE.I);
6.}). i = i;
7.}
8.}
3, add a layer of closure, I in the form of function parameters passed to the inner layer function
1. function Init3 () {
2. var pary = document.getElementsByTagName ("P");
3. for (var i=0; i<pary.length; i++) {
4. (function (ARG) {
5. Pary[i].onclick = function () {
6. Alert (ARG);
7.};
8.}) (i);//call-time parameter
9.}
10.}
4, add a layer of closure, I in the form of local variables passed to the inner layer function
1. function Init4 () {
2. var pary = document.getElementsByTagName ("P");
3. for (var i=0; i<pary.length; i++) {
4. (function () {
5. var temp = local variable at i;//call
6. Pary[i].onclick = function () {
7. Alert (temp);
8.}
9.}) ();
10.}
11.}
5, add a layer of closures, return a function as a response event (note the subtle difference from 3)
1. function Init5 () {
2. var pary = document.getElementsByTagName ("P");
3. for (var i=0; i<pary.length; i++) {
4. Pary[i].onclick = function (ARG) {
5. Return function () {//return a
6. Alert (ARG);
7.}
8.} (i);
9.}
10.}
6, with the function implementation, in fact, each generation of an instance of the functions will produce a closure
1. function Init6 () {
2. var pary = document.getElementsByTagName ("P");
3. for (var i=0; i<pary.length; i++) {
4. Pary[i].onclick = new Function ("alert (" + i + ");"); /new one function instance at a time
5.}
6.}
7, with the function to achieve, pay attention to the difference with 6
1. function Init7 () {
2. var pary = document.getElementsByTagName ("P");
3. for (var i=0; i<pary.length; i++) {
4. Pary[i].onclick = Function (' Alert (' +i+ ') ');
5.}
6.}
Deep understanding of how JavaScript's closure features add events to objects in a loop (reprinted)