WEB前端技巧之JQuery為動態添加的元素繫結事件

來源:互聯網
上載者:User

標籤:

 jquery 為動態添加的元素繫結事件

如果直接寫click函數的話,只能把事件綁定在已經存在的元素上,不能綁定在動態添加的元素上

可以用delegate來實現

  1. .delegate( selector, eventType, handler )

例如樣本:

  1. $(‘someUlSelector‘).delegate(‘someLiSelector‘,‘click‘,function(){
  2. //codes...
  3. //$(this) for the current jquery instance of the element
  4. });

jQuery 事件 - delegate() 方法

jQuery的delegate有好幾個缺憾

因為sizzle少提供了以refEl為參考、一個按selector來篩選els的filter(els,selector,refEl)的功能,jq需要自己去實作類別似的功能。

  • 其一:selector是基於:root的,而不是:scope的。所以,在寫代碼時,需要帶上 scrope 的定位部分。—-註::root指document根節點,:scope指代理節點

範例程式碼如

  1. <HTML><HEAD><TITLE>JK Test</TITLE>
  2. <METAcontent="text/html; charset=gb2312"http-equiv=Content-Type>
  3. <scriptsrc="http://common.cnblogs.com/script/jquery.js"type="text/javascript"></script>
  4. <style>
  5. div{border:1px solid balck;padding:5px;margin:2px;}
  6. </style>
  7. </HEAD>
  8. <body>
  9. <divid="div1">div1
  10. <divid="div1_1">div1_1
  11. <divid="div1_1_1">div1_1_1
  12. <spanstyle="color:red">span</span>
  13. </div>
  14. </div>
  15. <divid="div1_2">div1_2
  16. </div>
  17. </div>
  18. </body>
  19. <scripttype="text/javascript">
  20. $(‘#div1‘).delegate(‘#div1>*‘,‘click‘,function(){alert(this.innerHTML);});//點擊div1_1,div1_2或時,都有效。
  21. </script>
  22. </HTML>
  1. //例如,代理div1的兒子,需要這樣寫:
  2. $(‘#div1‘).delegate(‘#div1>*‘,‘click‘,fun});
  3. //而不是這樣寫:
  4. $(‘#div1‘).delegate(‘>*‘,‘click‘,fun});
  • 其二:多個祖先滿足條件時,只觸發了target的closest的那一個祖先,而不是都觸發。—-想不通jquery為什麼要採用這個策略

範例程式碼如

  1. <HTML><HEAD><TITLE>JK Test</TITLE>
  2. <METAcontent="text/html; charset=gb2312"http-equiv=Content-Type>
  3. <scriptsrc="http://common.cnblogs.com/script/jquery.js"type="text/javascript"></script>
  4. <style>
  5. div{border:1px solid balck;padding:5px;margin:2px;}
  6. </style>
  7. </HEAD>
  8. <body>
  9. <divid="div1">div1
  10. <divid="div1_1">div1_1
  11. <divid="div1_1_1">div1_1_1
  12. <spanstyle="color:red">span</span>
  13. </div>
  14. </div>
  15. <divid="div1_2">div1_2
  16. </div>
  17. </div>
  18. </body>
  19. <scripttype="text/javascript">
  20. $(‘body‘).delegate(‘div‘,‘click‘,function(){alert(this.innerHTML);});//點擊id1_1_1時,應該同時代理到三個div的點擊
  21. </script>
  22. </HTML>
  • 其三:與sizzle一樣,可能是沒有回溯,在某些情況下有bug。

例如,當span有多個祖先是div時,這個代理會失效:$(‘body‘).delegate(‘body>div span‘,‘click‘,fun;});
範例程式碼如

  1. <HTML><HEAD><TITLE>JK Test</TITLE>
  2. <METAcontent="text/html; charset=gb2312"http-equiv=Content-Type>
  3. <scriptsrc="http://common.cnblogs.com/script/jquery.js"type="text/javascript"></script>
  4. <style>
  5. div{border:1px solid balck;padding:5px;margin:2px;}
  6. </style>
  7. </HEAD>
  8. <body>
  9. <divid="div1">div1
  10. <divid="div1_1">div1_1
  11. <divid="div1_1_1">div1_1_1
  12. <spanstyle="color:red">span</span>
  13. </div>
  14. </div>
  15. <divid="div1_2">div1_2
  16. </div>
  17. </div>
  18. </body>
  19. <scripttype="text/javascript">
  20. $(‘body‘).delegate(‘body>div span‘,‘click‘,function(){alert(this.innerHTML);});//點擊span時,應該出現alert的
  21. </script>
  22. </HTML>

中文API的解釋delegate



來自為知筆記(Wiz)

WEB前端技巧之JQuery為動態添加的元素繫結事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.