jQuery 1.3 的 Live Event Delegration,以及速度問題

到公司跟 ManicjQuery 1.3 的時候才發現少提了一個很重要新功能:Live Event Delegration。

假設你對 #foo 裡的圖掛上一些 event:

$('#foo img').hover(f);

接下來你重新讀取 #foo:

$('#foo').load('/url/foo.php?page=' + num);

在 1.3 之前,本來的事件必須重新再掛一次:

$('#foo img').hover(f);

在 1.3 後,你可以一開始用 Live Event Delegration 掛上去,他會自己在裡面內容更新後重掛:

$('#foo img').live('hover', f);

這個功能可以少寫不少 code...

另外一點是速度問題。有人在 mailing list (Google Groups) 上回報掛上 Sizzle 後,FirefoxOpera 的速度反而比較慢:「Sizzle maybe slowing down DOM manipulations」,這個問題在 John Resig 確認後發現是 Sizzle 的 cache 問題後修正了:「Removing the caching code for Firefox and Opera - caused a severe negative performance impact for those browsers.」,所以在 1.3 Beta 2 的時候應該會感覺到更快。

2 thoughts on “jQuery 1.3 的 Live Event Delegration,以及速度問題”

  1. 跟據 John 在 Ajaxian 這篇的 reply,那其實不算是 event delegation,是比較簡單的。所以就未必有 event delegation 的好處,例如節省掛載 event 的數量。

Leave a Reply

Your email address will not be published. Required fields are marked *