The difference and use of bind (), Live () in jquery (event handling)

Source: Internet
Author: User

Original: The difference and use of bind (), Live () in jquery (event handling)

Using jquery for a while, when you start to look at someone else's source code, you have doubts about how the event is bound. Like what:

var btn=$ ("#button");  Btn.click (function() {  alert ("I have been clicked!" );  

This binds a click event.

1.bind () method parsing

But later found that JQUERYAPI also has the Bind method, has not been used, to see how the API use:

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

Binding multiple event types at the same time

function () {  $ (this). Toggleclass (' entered ');}); 

Note: The event type name is separated by a space, using the scene: Multiple event types are used for the same action.

The bind () method can bind multiple events relative to the direct registration event, and the same is all binding on the target object, this method is the longest, and it also solves the browser compatibility problem in event processing.

Note If the parent layer has binding events, you need to block bubbles, e.stoppropagation (), and e.cancellbubble ().

2.live () method parsing

The usage is as follows:

function () {  $ (this). Toggleclass (' entered');}); The/.live () method uses the concept of an event delegate to handle the binding of an event. 

The Live () method uses the concept of event delegates to handle the binding of events. It is the same as binding an event with. bind (). The difference is:

Live () Always find the top-level element (document) of the event object (Event.target), and event binding on the document node, and the event-bubbling mechanism for event delegation, using only one definition at a time to meet all of the required event objects, when using environments where multiple objects do the same type of thing For example, to have all the elements pop up their ID code as follows:

<DivID= "Parent">            <DivID= "Son">                <DivID= "grandson">fffffffff</Div>            </Div>           </Div>

We're going to have three Div pop up their IDs now.

The JS code is as follows:

$ ("#parent"). Live ("Click", Function () {     alert (this). id);     })        

The difference and use of bind (), Live () in jquery (event handling)

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.