jquery Cyclic binding Event

Source: Internet
Author: User

<script type= "Text/javascript" src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></ Script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
var array = [0, 1, 2, 3];

                 for (var index in array) {
                     $ ("#btn" + index). Click (function () {
                         var item  = Array[index];
                         alert (item);
                //Always pops 3 because function () {} is not parsed immediately until the call is resolved, at which point the index is already 3.

                 for (var index in array) {
                     $ ( "#btn" + index). Click (function (i) {
                         var item  = array[i];
                         alert (item);
                    } (index));
                //immediately eject 0, 1, 2, 3 because the function () {} (index) is immediately parsed, and when alert is immediately available, it bounces out.

               /*for (var index in Array) {
                     $ ("#btn" + index). Click (function (i) {
                         return function () {
                             var item = Array[i];
                             alert (item);
                    } (index));
                //execute correctly, click Btn0, Pop 0, click Btn1, Pop-up 1 ...
               //1. Because function (i {} (index) is resolved immediately, so I was sent to 0, 1, 2, 3, in sequence.
                //2. There is no direct alert inside, because do not want to execute immediately, want to click to execute again, so return a function to go out.

                for (var index in array) {
                     $ ("#btn" + Index). bind ("click", {Index:index}, ClickHandler);

                function ClickHandler (event) {
                     var index = Event.data.index;
                     var item = Array[index];
                     alert (item);
               //execute correctly, click Btn0, Pop 0, click btn1, Pop 1 ...
               // The event.data is used because the index is persisted to the event.data when it is bound, so we can take it when responding.

        <input type= "button" id= "Btn0" value= "Btn0"/>
         <input type= "button" id= "Btn1" value= "btn1"/>
         <input type= "button" id= "btn2" value= "btn2"/>
         <input type= "button" id= "Btn3" value= "Btn3"/>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.