Example code for JavaScript cyclic binding events _javascript Tips

Source: Internet
Author: User
For example, a list of indeterminate lengths that changes the background when the mouse passes through a bar.
<textarea id="runcode61344"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <title>untitled page</title> </p ead> <body> <ul id= "list" > <li> 1th record </li> <li> 2nd record </li> <li> 3rd record </ li> <li> 4th record </li> <li> 5th record </li> <li> 6th record </li> </ul> <script type= "Text/javascript" > var list_obj = document.getElementById ("list"). getElementsByTagName ("Li"); Gets the object array for all Li below the list for (var i = 0; I <= list_obj.length i++) {list_obj[i].onmousemove = function () {This.style. BackgroundColor = "#cdcdcd"; } list_obj[i].onmouseout = function () {This.style.backgroundColor = "#FFFFFF"; }} </script> </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

This example loops through a set of object binding event handler functions.
However, if we add some demand on this basis. For example, when you click on a record, what's the first few records?
Ken, you'll take it for granted:
<textarea id="runcode14037"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <title>untitled page</title> </p ead> <body> <ul id= "list" > <li> 1th record </li> <li> 2nd record </li> <li> 3rd record </ li> <li> 4th record </li> <li> 5th record </li> <li> 6th record </li> </ul> <script type= "Text/javascript" > var list_obj = document.getElementById ("list"). getElementsByTagName ("Li"); Gets the object array for all Li below the list for (var i = 0; I <= list_obj.length i++) {list_obj[i].onmousemove = function () {This.style. BackgroundColor = "#cdcdcd"; } list_obj[i].onmouseout = function () {This.style.backgroundColor = "#FFFFFF"; List_obj[i].onclick = function () {alert ("This is the first" + i + "record"); }} </script> </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Test it out and you'll find out that alert is all: This is the 6th record.
In fact, here the For Loop has cycled through the entire list and executed the i++, so here I turned 6,
Is there any good way to solve the problem?
That's closure, and personally think that closure is one of the most elusive places in JS,
Look at what a closure is:
A closure is an inner function that refers to a variable that exists within a function that surrounds him, even though the execution of the outer function is terminated.
In this example we can do this:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <title>untitled page</title> </p ead> <body> <ul id= "list" > <li> 1th record </li> <li> 2nd record </li> <li> 3rd record </ li> <li> 4th record </li> <li> 5th record </li> <li> 6th record </li> </ul> <script type= "Text/javascript" > Function tt (NOB) {this.clickfunc = function () {alert ("This is the first" + (NOB + 1) + "record"); } var list_obj = document.getElementById ("list"). getElementsByTagName ("Li"); Gets the object array for all Li below the list for (var i = 0; I <= list_obj.length i++) {list_obj[i].onmousemove = function () {This.style. BackgroundColor = "#cdcdcd"; } list_obj[i].onmouseout = function () {This.style.backgroundColor = "#FFFFFF"; var col = new TT (i); List_obj[i].onclick = Col.clickfunc; } </script> </boDy> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

PS: Closure is difficult, very complicated!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.