JQuery event delegate () Usage

Source: Internet
Author: User

Delegate definition and usage

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

The event handler using the delegate () method applies to current or future elements (such as new elements created by scripts ).

Parameters Description
ChildSelector Required. Specifies one or more child elements of the event handler to be appended.
Event

Required. Specifies one or more events to be appended to an element.

Multiple event values are separated by spaces. Must be a valid event.

Data Optional. Specifies additional data to be passed to the function.
Function Required. Specifies the function that runs when an event occurs.

Syntax
$ (Selector). delegate (childSelector, event, data, function)

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

Overview

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

Parameters
Selector, [type], fnString, String, Function V1.4.2
Selector: Specifies the selector string used by the filter to trigger events.

Type: one or more events appended to an element. Multiple event values are separated by spaces. Must be a valid event.

Fn: the function that runs when an event occurs.

Selector, [type], [data], fnString, String, Object, Function V1.4.2
Selector: Specifies the selector string used by the filter to trigger events.

Type: one or more events appended to an element. Multiple event values are separated by spaces. Must be a valid event.

Data: additional data passed to the Function

Fn: the function that runs when an event occurs.

Selector, eventsString, String V1.4.3
Selector: Specifies the selector string used by the filter to trigger events.

Events: one or more event-type string and function data ing to execute them.

Example
Description:
When you click the mouse, the p element is hidden or displayed:

HTML code:Copy codeThe Code is as follows: <div style = "background-color: red">
<P> This is a paragraph. </P>
<Button> click here </button>
</Div>

JQuery code:Copy codeThe Code is as follows: $ ("div"). delegate ("button", "click", function (){
$ ("P"). slideToggle ();
});

Description: The delegate method can be used as an alternative to the live () method, so that each event is bound to a specific DOM element.
The following two pieces of code are equivalent:Copy codeThe Code is as follows: $ ("table"). delegate ("td", "hover", function () {$ (this). toggleClass ("hover ");
}); $ ("Table "). each (function () {$ ("td", this ). live ("hover", function () {$ (this ). toggleClass ("hover ");});
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.