<html>
              
          <head>
                  
          <title></title>
                  
          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
              
          </head>
              
          <body>
                  
          <script type="text/javascript">
                      $(document).ready(
          function () {
                          
          var array = [0123];

                          
          // 1.
                          /*
                          for(var index in array) {
                              $("#btn" + index).click(function() {
                                  var item  = array[index];
                                  alert(item);
                              });
                          }
          */
                          
          // 始終彈出3, 因為function() {} 并沒有被立即解析,直到調(diào)用的時候才被解析,這時index已經(jīng)是3了。


                          
          // 2.
                          /*
                          for(var index in array) {
                              $("#btn" + index).click(function(i) {
                                  var item  = array[i];
                                  alert(item);
                              }(index));
                          }
          */
                          
          // 立即彈出0, 1, 2, 3,因為使用了function() {}(index)立即被解析,遇到alert,就立即彈出來了。


                          
          // 3.
                          /*for (var index in array) {
                              $("#btn" + index).click(function (i) {
                                  return function () {
                                      var item = array[i];
                                      alert(item);
                                  };
                              } (index));
                          }
          */
                          
          // 正確執(zhí)行,點擊btn0,彈出0,點擊btn1,彈出1...
                          // 1.因為function(i) {}(index)是被立即解析的,所以i依次送入的是0, 1, 2, 3
                          // 2.內(nèi)部沒有直接alert,是因為不想立即執(zhí)行,想點擊時再執(zhí)行,所以返回了一個函數(shù)出去。


                          
          // 4.
                          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);
                          }
                          
          // 正確執(zhí)行,點擊btn0,彈出0,點擊btn1,彈出1...
                          // 利用了event.data,因為index在綁定的時候已經(jīng)被持久化到event.data中了,所以響應(yīng)的時候我們可以取到。
                      });
                  
                  
          </script>

                  
          <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" />        
              
          </body>
          </html>
          posted on 2010-08-05 13:39 sanmao 閱讀(2638) 評論(1)  編輯  收藏

          FeedBack:
          # re: jquery循環(huán)綁定事件
          2014-06-12 16:28 | 凌渡辰風(fēng)
          疑惑了很久的,看到此博文,理解了問題出現(xiàn)的原因!
          贊一個~  回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 丹江口市| 封开县| 商洛市| 开江县| 徐州市| 泽库县| 本溪| 沾益县| 桓台县| 清河县| 烟台市| 剑川县| 祁门县| 腾冲县| 南通市| 连州市| 韩城市| 康平县| 惠安县| 德庆县| 安阳县| 乌鲁木齐县| 天水市| 孙吴县| 土默特右旗| 正蓝旗| 岳普湖县| 山东省| 南开区| 芒康县| 东阿县| 肥乡县| 雅安市| 定远县| 革吉县| 泗洪县| 民乐县| 宿迁市| 清新县| 威信县| 大洼县|