<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() {} 并沒有被立即解析,直到調用的時候才被解析,這時index已經是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));
                          }
          */
                          
          // 正確執行,點擊btn0,彈出0,點擊btn1,彈出1...
                          // 1.因為function(i) {}(index)是被立即解析的,所以i依次送入的是0, 1, 2, 3
                          // 2.內部沒有直接alert,是因為不想立即執行,想點擊時再執行,所以返回了一個函數出去。


                          
          // 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);
                          }
                          
          // 正確執行,點擊btn0,彈出0,點擊btn1,彈出1...
                          // 利用了event.data,因為index在綁定的時候已經被持久化到event.data中了,所以響應的時候我們可以取到。
                      });
                  
                  
          </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 閱讀(2639) 評論(1)  編輯  收藏

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

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


          網站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 柘荣县| 花莲市| 南开区| 南宫市| 班戈县| 珲春市| 龙胜| 武夷山市| 弥渡县| 滁州市| 古交市| 舞阳县| 诸城市| 临高县| 黄龙县| 鲜城| 安仁县| 宝清县| 扎赉特旗| 大宁县| 惠安县| 广平县| 奎屯市| 广元市| 临泽县| 周口市| 东光县| 长沙市| 化德县| 湛江市| 南开区| 祁阳县| 莎车县| 桦甸市| 双峰县| 昭觉县| 同德县| 乌拉特中旗| 寿阳县| 本溪市| 固安县|