Home  >  Article  >  Web Front-end  >  Sample code for Javascript loop binding events_javascript tips

Sample code for Javascript loop binding events_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:59:53879browse

For example: a list of indefinite length, the background changes when the mouse passes over a certain item.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

This example loops into a group Object binding event handler function.
However, if we add some requirements on this basis. For example, when you click on a certain record, what record is this?
Maybe you will write this as a matter of course:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

Test it and you will find alert What comes out is: This is the 6th record
In fact, the for loop here has cycled through the entire list and executed i, so i here becomes 6.
Is there any good way to solve this problem? ?
That’s closure. Personally, I think closure is one of the most elusive things in js.
Look at what closure is:
Closure means that inner functions can reference existing and variables within the enclosing function, even if execution of the outer function has terminated.
In this example we can do this:
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh it to execute
]PS: Closure is difficult , very complicated!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn