我的情況下什麼是JavaScript事件委託,以及如何讓我的js DRY?
$(".myElementWithSpecialActionHandler").click(function(){});
元素搬遷後,說插入到頁面的不同位置,
var content_from_table = from_table_obj.html();
var content_target_table = to_table_obj.html();
Ajax調用之前。我把
from_table_obj.html(content_target_table);
to_table_obj.html(content_from_table);" in the ajax response handler.
之後,事件處理函數停止工作。所以我必須做
// after DOM manipulation
// again >.<
$(".myElementWithSpecialActionHandler").click(function(){});
是event delegation唯一能解決這個嗎?或者有更好的方法來避免這種情況?
編輯:增加了「移居」代碼
*元素被重新定位是什麼意思*? –
你如何「重新定位」元素?使用任何相關的方法append()或prepend()(當元素已經存在時移動元素),處理程序將保持不變。因此,發佈您正在使用的移動元素 –
@ArunPJohny Editted的相關代碼。感謝您的協助 – noooooooob