Introduction to delegate () usage in jquery

Source: Internet
Author: User

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

delegate definition and usage
The delegate () method adds one or more event handlers for the specified element (the child element of the selected element) and specifies the function to run when these events occur.
Event handlers that use the delegate () method apply to current or future elements, such as new elements created by a script.  

Parameters Description
Childselector Necessary. Specifies one or more child elements to attach the event handler.
Event

Necessary. Specifies one or more events that are attached to an element.

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

Data Optional. Specifies additional data that is passed to the function.
function Necessary. Specifies the function to run when an event occurs.



Grammar
$ (selector). Delegate (Childselector,event,data,function)

Return value: JQuery delegate (SELECTOR,[TYPE],[DATA],FN)

Overview

The specified element (child elements that belong to the selected elementAdds one or more event handlers, and specifies the function to run when these events occur.

Parameters
Selector,[type],fnstring,string,function V1.4.2
Selector: selector string for the element that the filter triggers the event.
Type: One or more events attached to the element. Multiple event values are separated by a space. Must be a valid event.
fn: A function that runs when an event occurs 
Selector,[type],[data],fnstring,string,object,function V1.4.2< Span class= "Apple-converted-space" >&NBSP;
Selector: selector string for the element that the filter triggers the event. &NBSP;
Type: One or more events attached to the element. Multiple event values are separated by a space. Must be a valid event.  ,
Data: Additional data passed to the function &NBSP;
fn: Functions that run when an event occurs &NBSP;
selector,eventsstring,string v1.4.3&NBSP;
Selector: selector string for the element that the filter triggers the event. &NBSP;
Events: A data map of one or more event type strings and functions to execute them. &NBSP;

Example :&NBSP;
hides or displays the P element when the mouse is clicked: &NBSP;

HTML Code: Span class= "Apple-converted-space" >&NBSP;

 <  div        >  <  p  >  this is a paragraph. </ p         >  <  button  >  click here </ button   >  </ div  >  


JQuery code: &NBSP;

 $ ("div"). Delegate ("button", "click", function  () {$ ("P"). Slidetoggle (); }); 


Description: Delegate This method can be used as an alternative to the live () method, allowing each event to be bound to a specific DOM element. &NBSP;
The following two-segment code is equivalent: &NBSP;

function () {$ (this). Toggleclass ("hover");  }); $ ("table"). each (thefunctionthisfunction() {$(this). Toggleclass ("hover");});  

Introduction to delegate () usage in jquery

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.