delegate 可用來為目前或未來符合條件的子元素註冊事件,常見於動態產生的列表或刪除按鈕。
$(selector).delegate(childSelector, type, data, function)
listNode.delegate('.condition-remove', 'click', function (e) {
e.preventDefault();
$(this).parents('.search-condition-item').remove();
});
delegate 的事件來源需符合 childSelector,且事件會由共同父元素代為處理。若元素在 delegate 之後才加入,仍可正常觸發,這也是事件委派的優勢。