What beginners often encounter is getting HTML element sets and adding events to elements cyclically. Obtain the corresponding index in the Event Response Function (eventhandler. However, each query is the index of the last loop. The reason is that beginners do not understand the closure feature of JavaScript. When I was writing front-end js Code a few days ago, I encountered a traversal element to add a click event to it. That's why I spent the whole afternoon. Finally, I went home from work and checked the information online to find out how to solve the problem. (PS: I have also read the content about the loop binding handler in the fourth edition of jQuery basic tutorial. I didn't think it was easy at that time, so I didn't remember it .)
If you know this kind of situation, you can close the window and write it to the white ones like me. Thank you!
First, paste an incorrect example for your reference. (JQuery is used in this example. Import the jQuery library)
The Code is as follows:
Loop binding Handler