jQuery delegate() Method Explained
The delegate() method is used to bind events to child elements matching a selector on the parent element. Its syntax format is as follows:
$(parentElement).delegate(selector, eventType, handler);
The parentElement is the parent element to which the events are being bound, the selector is the selector for the matching child elements, eventType is the type of event (such as click, mouseover, etc.), and handler is the event handler function.
For example, the following code will bind a click event to all child elements with the class “child” under the parent element with the id “parent”:
$('#parent').delegate('.child', 'click', function() {
// 处理点击事件的逻辑
});
Note: The delegate() method has been deprecated after jQuery 3.0, it is recommended to use the on() method as a replacement. Therefore, the example above can also be rewritten as:
$('#parent').on('click', '.child', function() {
// 处理点击事件的逻辑
});