Differences between the live and bind methods in jQuery

Source: Internet
Author: User

Differences between the live and bind methods in jQuery

This article mainly analyzes the differences between the live and bind methods in jQuery in detail. If you need them, you can refer to them and hope to help you.

Note: If the layer and object added through jq must use live (), none of them work.

 

The disadvantage of live is that it does not release space after running. Too much use will occupy more memory, and bind () will release space after clicking it.

 

Difference 1:

 

Click here

 

You can bind a simple click event to this element:

 

$ ('. Clickme'). bind ('click', function (){

$ ('Body'). append ('

 

Another target

');

});

 

When an element is clicked, a warning box is displayed. Then, imagine that another element was added.

 

Although this new element can also match the selector ". clickme", clicking this element will not work because it is added after. bind () is called.

 

. Live () provides a method for this situation. If we bind a click event like this:

 

$ ('. Clickme'). live ('click', function (){

Alert ("Live handler called .");

});

 

Then add a new element:

 

$ ('Body'). append ('

 

Another target

');

 

Then, click the new element to trigger the event processing function.

 

 

 

Difference 2:

 

(1) The bind method can bind any JavaScript event, while the live method only supports click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, and mouseover in jQuery1.3, and mouseup. in jQuery 1.4.1, the focus and blue events are even supported (ing to focusin and focusout that are more appropriate and can be bubbling ). In addition, hover (mapped to "mouseenter mouseleave") is also supported in jQuery 1.4.1 ").

 

(2) live () does not fully support elements found through DOM traversal. Instead, the. live () method should always be used directly after a selector.

 

(3) When an element binds an event using the live method, if you want to prevent the event from being transmitted or bubbling, you must return false in the function and only call stopPropagation () it is impossible to prevent the passing or bubbling of events.

 

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.