I believe everyone has encountered a similar problem, the For loop binding event last execution is the same as the last event, such as this code
<body><ul id= "List" ><li>1</li><li>2</li><li>3</li><li>4 </li><li>5</li></ul><script>var list_obj = document.getElementsByTagName (' Li '); for (var i = 0; I <= list_obj.length; i++) { function() { alert (i);} } </script></body>
When clicked, the popup is not 0 1 2 3 4, but 5.
There are many solutions, such as creating a new function to set up a private method, and then each time new object is then bound to its method.
There is the closure of the package, the wording is relatively simple:
<body><ul id= "List" ><li>1</li><li>2</li><li>3</li><li>4 </li><li>5</li></ul><script>var list_obj = document.getElementsByTagName (' Li '); for (var i = 0; I <= list_obj.length; i++) { (function() { var p =< c10> i function() { alert (p); } }) ();} </script></body>
is to add a closure to each loop and then set a temporary variable to solve the problem.
JS Loop-bound Event solution