jquery event binding on (), bind () and delegate () methods

Source: Internet
Author: User

Chew for a period of time JS related, learn the process found in jquery in the binding event, someone with bind (), someone with on (), someone with delegate (), and someone with live (), see the code when the realization of the function is also overturned, Just have not fully understand the difference between, so today checked the next information, make a summary of their own.

There are so many types of binding methods because the reason for the version update of jquery, such as the On () method, occurs after 1.7.

The jquery Event binding API page mentions that the live () method is obsolete and is not recommended for use. So here we mainly look at the following three methods: Bind (), delegate (), on ()

We prepare an HTML page for testing of various types of event bindings.

  

A simple page, placed a div,div inside a number of P elements and a button, click the button can append P element. We will bind the click event to the P element on the page below.

Bind ()

Usage

function () {    alert (this). Text ());})

This binds the Click event to the P element in all div and responds with the popup content. Binding is simple and fast, but there are two problems:
First, the implicit iterative approach is used, and if the number of elements to be matched is particularly high, such as if I put 50 p elements in the Div, I have to perform a binding 50 times. For a large number of elements, performance is affected.
But if the ID selector, because the ID is unique, with the bind () method is very fast.
The second problem is that you cannot bind to an element that does not already exist. Click on the button on the page, will dynamically add a P element, click on this P element, you will find no action response.
These two problems can be solved by using the delegate method.

In addition, the bind () method also has a shorthand, the above code can also be replaced by:

$ ("div p"). Click (function  () {    alert (this). Text ());})

Delegate ()

Usage

function () {    alert (this). Text ());});

This approach employs the concept of event delegation. Instead of binding the event directly for the P element, it binds the event to its parent element (or ancestor element), and when clicked on any element within the Div, the event bubbles up from the event target up to the element that you are binding the event to, such as the DIV element in the example. During bubbling, code is executed if the currenttarget of the event matches the selector.

This solves the above two problems with the bind () method, without having to bind the event to the P element, or binding the P element that is added dynamically. Even if you bind the event to a document, you do not have to wait for the document to be ready to execute the binding.

This way, binding is easy, but there may be problems when calling. If the event target is in a deep position in the DOM tree, this layer bubbles up to find the element that matches the selector, and it affects performance.

On ()

On () In fact, the previous binding event method is unified, see jquery uncompressed source code (I see the version is 1.11.3), you can find whether bind () or delegate () is actually implemented by the on () method, but the parameters are different.

Bindfunction(types, data, fn) {return  This. On (types,NULL, data, FN); }, Unbind:function(types, fn) {return  This. Off (types,NULL, FN); }, Delegate:function(selector, types, data, fn) {return  This. On (types, selector, data, FN); } undelegate:function(selector, types, fn) {//(namespace) or (selector, types [, fn])     returnArguments.length = = = 1? This. Off (Selector, "* *"): This. Off (types, selector | | "**", FN); }

In the example above, you can use on () to bind as follows:

$ ("div"). On ("click", "P",function() {    alert (). Text ());})

Official documentation Recommendations:


As of JQuery 1.7, the. On () method was the preferred method for attaching event handlers to a document.

Use on () to bind the event as much as possible.

removing events

Corresponds to the bind (), delegate (), and on () binding methods, which remove events by:

In addition to removing the specified event bindings as shown above, you can remove all event bindings without passing in parameters, which are not listed here, and the official jquery documentation is very detailed.

Summarize

1. The selector matches the elements more than a few, do not use bind () iteration binding

2. When using the ID selector, bind () can be used

3. You need to bind the dynamically added elements with delegate () or on ()

4. Using the delegate () and on () methods, the DOM tree is not too deep

5. Use on () as much as possible

jquery event binding on (), bind () and delegate () methods

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.