Question about the use of delegate in jQuery, jquerydelegate

Source: Internet
Author: User

Question about the use of delegate in jQuery, jquerydelegate

I am used to bind and live, so I am not used to delegate...

You can bind events to dynamically generated tag elements. Maybe live and delegate are supported. However, live is not supported in the new version, and only delegate is supported.

Delegate is really special. It is different from the Binding style of other events.

This is because we get used to the previous bind style.

To put it simply, it is the general idea.

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 ).

Syntax

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

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.

For example, this small code.

<Html> 

I have always written

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

The sub-Selector does not need to be selected ..

Otherwise, an unknown error occurs as I did (Clicking will trigger the click, but clicking other elements will also trigger the click ...)

The above is all the content of this article. I hope you will like it.

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.