Usage and Difference Analysis of bind, live, delegate and one methods in jQuery

Source: Internet
Author: User

The bind () method is used to attach a handler to an event of each Matching Element and return the jQuery object.

. Bind (eventType [, evnetData], Handler (eventObject ))

The eventType parameter is a string that contains one or more javaScript event types, such as click, submit, or custom event names. Multiple event types are separated by spaces when multiple event types are specified; eventData is of the Map type and provides the data to be passed to the event handler. handler specifies the function to be executed when the event is triggered, and eventObject indicates the event object.

The. bind () method attaches the event handler to an eventType event that matches each element in the element set. If necessary, it can also pass data to the event handler.

The live () method attaches an event handler to a specified event that matches all elements of the current selector (including existing or future additions) and returns the jQuery object.

. Live (eventType, [eventData], handler)

The eventType parameter is a string that contains one or more javaScript event types, such as click, keydown, or custom event names. eventData is an optional parameter and is of the Map type, the data to be passed to the event handler. this parameter is added in jQuery1.4. handler is a function that will be executed when the event is triggered.

. The live () method attaches the event handler to the eventType event of each Matching Element (including the existing and future additions, if necessary, you can also use eventData to transmit data to the event handler.

. The live () method is basic. A deformation of the bind () method. The latter can append the event handler to the element when calling. when bind (), the elements matching the jQuery object will be appended to the event handler, but the elements to be added later will not be appended to the event handler. Therefore, you also need to call these elements again. bind () method.

The. one () method attaches the event handler to the specified event of the matching element and returns the jQuery object. The attached event handler can be executed only once at most.

. One (eventType, [eventData], handler (eventObject ))

The eventType parameter is a string that contains one or more javaScript event types, such as click, submit, or custom event names. Multiple event types are separated by spaces when multiple event types are specified; eventData is of the Map type and provides the data to be passed to the event handler. handler specifies the function to be executed when the event is triggered, and eventObject indicates the event object.

The. one () method is similar to. bind (). The difference is that the event handler bound with. one () will be automatically unbound once it is executed.

The. delegate () method attaches the handler to one or more events that match all elements of the selector (existing or future) based on a specific set of root elements.

. Delegate (selector, eventType [, eventData], handler)

The selector parameter is a selector used to filter the elements that trigger events. eventType is a string that specifies one or more JavaScript event types (multiple events are separated by spaces), such as click, keydown or custom event name; eventData is the ing type, which indicates the data to be passed to the event handler; handler indicates the function executed when the event is triggered.

. Delegate () is similar to. live (). You can delegate the binding of each event to a specified DOM element.

Related Article

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.