Home  >  Article  >  Web Front-end  >  How to use jQuery delegate() event

How to use jQuery delegate() event

黄舟
黄舟Original
2017-06-26 09:55:211121browse

The

delegate() method adds one or more event handlers to the specified element (a child element of the selected element) and specifies the functions to be run when these events occur

delegate definition and usage

delegate() method adds one or more event handlers to the specified element (belonging to the child elements of the selected element) and specifies when these events occur function to run.

Event handlers using the delegate() method apply to the current or future elements (such as new elements created by scripts).

Parameters Description
childSelector Required. Specifies one or more child elements to which event handlers are attached.
event

Required. Specifies one or more events to attach to the element.

Separate multiple event values ​​by spaces. Must be a valid event.

data Optional. Specifies additional data to be passed to the function.
function Required. Specifies a function to run when an event occurs.



Syntax

$(selector).delegate(childSelector,event,data,function)

Return value: jQuery delegate(selector,[type], [data],fn)

Overview

Add one or more event handlers to the specified element (a child element of the selected element) and specify the functions to run when these events occur .

Parameters
selector,[type],fnString,String,Function V1.4.2
selector: selectorString, used for filter The element that triggered the event.

type: One or more events attached to the element. Multiple event values ​​separated by spaces. Must be a valid event.

fn: Function that runs when the event occurs
selector,[type],[data],fnString,String,Object,Function V1.4.2
selector: Selector string, used for The element on which the filter triggers the event.

type: One or more events attached to the element. Multiple event values ​​separated by spaces. Must be a valid event.

data: Extra data passed to the function

fn: Function to run when the event occurs

selector, eventsString, String V1.4.3
selector: selector String for the element on which the filter triggered the event.

events: A data map of strings and functions of one or more event types to execute them.

Example
Description:
Hide or show the p element when the mouse is clicked:

HTML code:

<p style="background-color:red"> 
<p>这是一个段落。</p> 
<button>请点击这里</button> 
</p>

jQuery code:

$("p").delegate("button","click",function(){ 
$("p").slideToggle(); 
});


Description: The delegate method can be used as an alternative to the live() method, allowing each event to be bound to a specific DOM element.
The following two pieces of code are equivalent:

$("table").delegate("td", "hover", function(){ $(this).toggleClass("hover"); 
});$("table").each(function(){ $("td", this).live("hover", function(){ $(this).toggleClass("hover"); }); 
});


The above is the detailed content of How to use jQuery delegate() event. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn