Summary: Event binding in jQuery, summary jquery binding

Source: Internet
Author: User

Summary: Event binding in jQuery, summary jquery binding
Preface

Because jquery is often used in projects to perform addition, deletion, and deletion of dom elements, the binding event Method of dom elements is involved. The differences between bind, live, delegate, and on are briefly summarized, I hope this article will help my friends in the future. If there is anything wrong with it, I hope to point it out and communicate with you.

I. bind ()

Brief Description

  Bind ()Add one or more event processors to the matching element.

Usage

  $ (Selector). bind (event, data, function)

Event:Required. One or more events added to an element, such as click and dblclick;

Single Event Processing: for example$ (Selector). bind ("click", data, function );

Multi-event processing: 1. Separate multiple events with spaces, for example$ (Selector). bind ("click dbclick mouseout", data, function );

2. Use braces to flexibly define multiple events, such$ (Selector). bind ({event1: function, event2: function ,...})

3. Space separation: binding is rigid and cannot bind functions to events separately. It is suitable for handling multiple events and calling the same function;

Alternative to braces: binding is flexible and functions can be bound to events separately;

Data:Optional. parameters to be passed;

Function:Required. The function to be executed when the binding event occurs;

Example

View Code

Applicable to Jquery

Applicable to All VersionsBut according to the official website, since jquery1.7 and later, bind () functionWe recommend that you use on () instead.

Ii. Live ()

Brief Description

  Live ()DirectionCurrent or future matching elementsAdd one or more event processors;

Usage

$ (Selector). Live (Event,Data,Function)

Event:Required. One or more events added to an element, such as click and dblclick;

Single Event Processing: for example$ (Selector). live ("click", data, function );

Multi-event processing: 1. Separate multiple events with spaces, for example$ (Selector). live ("click dbclick mouseout", data, function );

2. Use braces to flexibly define multiple events, such$ (Selector). live ({event1: function, event2: function ,...})

3. Space separation: binding is rigid and cannot bind functions to events separately. It is suitable for handling multiple events and calling the same function;

Alternative to braces: binding is flexible and functions can be bound to events separately;

Data:Optional. parameters to be passed;

Function:Required. The function to be executed when the binding event occurs;

Example

View Code

Applicable to Jquery

Jquery1.9Supported in versions below,Jquery1.9 and later versionsDeleted this method,Jquery1.9 and later versions are replaced by the on () method.

3. delegate ()

Brief Description

  Delegate ()Add one or more event handlers for the specified Element (child element of the selected element) and specify 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 ).

Usage

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

  ChildSelector:Required. The element of the event handler, which is generally a child element of selector;

Event:Required. One or more events added to an element, such as click and dblclick;

Single Event Processing: for example$ (Selector). delegate (childselector, "click", data, function );

Multi-event processing: 1. Separate multiple events with spaces, for example$ (Selector). delegate (childselector, "click dbclick mouseout", data, function );

2. Use braces to flexibly define multiple events, such$ (Selector). delegate (childselector, {event1: function, event2: function ,...})

3. Space separation: binding is rigid and cannot bind functions to events separately. It is suitable for handling multiple events and calling the same function;

Alternative to braces: binding is flexible and functions can be bound to events separately;

Data:Optional. parameters to be passed;

Function:Required. The function to be executed when the binding event occurs;

Example

View Code

Applicable to Jquery

Jquery1.4.2 and later versions;

Iv. on ()

Brief Description

  On ()Adds one or more event handlers for the specified Element and specifies the function to run when these events occur. The event handler using the on () method applies to current or future elements (such as new elements created by the script ).

Usage

$ (Selector). on (event, childselector, data, function)

Event:Required. One or more events added to an element, such as click and dblclick;

Single Event Processing: for example$ (Selector). on ("click", childselector, data, function );

Multi-event processing: 1. Separate multiple events with spaces, for example$ (Selector). on ("click dbclick mouseout", childseletor, data, function );

2. Use braces to flexibly define multiple events, such $ (Selector). on ({event1: function, event2: function,...}, childselector );

3. Space separation: binding is rigid and cannot bind functions to events separately. It is suitable for handling multiple events and calling the same function;

Alternative to braces: binding is flexible and functions can be bound to events separately;

  ChildSelector:Optional. The element of the event handler, which is generally a child element of selector;

Data:Optional. parameters to be passed;

Function:Required. The function to be executed when the binding event occurs;

Example

View Code

Applicable to Jquery

Jquery1.7Jquery1.7 and later versionsUsed to replace bind () and live () event binding methods;

V. Differences and advantages and disadvantages of the four methods

Similarities:

1. All events of a single element and multiple events can be bound. The space is separated or the braces are replaced;

2. All events are passed to the document for event response through event bubbling;

6. Comparison and contact:

 

1. the bind () function can only set events for existing elements. However, live (), on (), and delegate () support setting events for new elements in the future. The Demo code is as follows:

View Code

 

2. the bind () function was well-respected before jquery1.7. After jquery1.7 was released, it was not officially recommended to use bind () instead of on (), this is also a new function added in version 1.7.

Used to replace the live () function. The live () function has been deleted in version 1.9;

 

3. The live () function is similar to the delegate () function, but the live () function is more efficient in execution speed, flexibility, and CSS selector support than delegate ().

 

4. bind () supports all Jquery versions; live () supports jquery1.8-; delegate () supports jquery1.4.2 +; on () supports jquery1.7 +; 

 

VII. Summary

If the jquery version referenced in the project is of a lower version, we recommend using delegate (). Later jquery versions can be replaced by on (). The above is only my opinion. If you have different ideas, please feel free to contact us.

 

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.