jQuery delegate () method
Examples
When you click the <div> element inside the <p> element, change the background color of all <p> element:
$("div").delegate("p","click",function(){
$("p").css("background-color","pink");
});
$("p").css("background-color","pink");
});
try it"
Definition and Usage
delegate () method for the specified elements (elements belonging to the selected sub-element) to add one or more event handlers, and provides a function to run when these events occur.
Event handler delegate () method applies to the current or future element (such as a new element created by the script).
JQuery from version 1.7 onwards, ON () method is the preferred method is to add the selected element event handlers.
grammar
$(selector).delegate(childSelector,event,data,function)
参数 | 描述 |
---|---|
childSelector | 必需。规定要添加事件处理程序的一个或多个子元素。 |
event | 必需。规定添加到元素的一个或多个事件。 由空格分隔多个事件值。必须是有效的事件。 |
data | 可选。规定传递到函数的额外数据。 |
function | 必需。规定当事件发生时运行的函数。 |
More examples
Add an event handler to the next element
How to use the delegate () method to add an event handler to the element has not been created.
Data passed to the function
How to name a custom event handler pass data.