.delegate()
Description: Attach a handler to one or more events for
all elements that match the selector, now or in the future, based on a
specific set of root elements.
.delegate( selector, eventType, handler(eventObject) )
selectorA selector to filter the elements that trigger the event.eventTypeA string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.handler(eventObject)A function to execute at the time the event is triggered.-
version added: 1.4.2.delegate( selector, eventType, eventData, handler(eventObject) )
selectorA selector to filter the elements that trigger the event.eventTypeA string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.eventDataA map of data that will be passed to the event handler.handler(eventObject)A function to execute at the time the event is triggered. -
version added: 1.4.3.delegate( selector, events )
selectorA selector to filter the elements that trigger the event.eventsA map of one or more event types and functions to execute for them.As of jQuery 1.7,.delegate()
has been superseded by the .on() method. For earlier versions, however, it remains the most effective means to use event delegation. More information on event binding and delegation is in the .on() method. In general, these are the equivalent templates for the two methods:$(elements).delegate(selector, events, data, handler); // jQuery 1.4.3+ $(elements).on(events, selector, data, handler); // jQuery 1.7+
.delegate()
code:
$("table").delegate("td", "click", function() { $(this).toggleClass("chosen"); }); - is equivalent to the following code written using
.on()
:
$("table").on("click", "td", function() { $(this).toggleClass("chosen"); });
To remove events attached withdelegate()
, see the .undelegate() method.
Passing and handling event data works the same way as it does for.on()
.
No comments:
Post a Comment