JQuery's. live () and. die ()

Source: Internet
Author: User

What is. live ()
. The live method is similar. bind (). In addition, it allows you to bind events to DOM elements. You can bind events to elements that do not exist in the DOM. Take a look at the following example:

For example, when a user clicks a link and wants to prompt that they are leaving the site.Copy codeThe Code is as follows: $ (document). ready (function (){
$ ('A'). click (function (){
Alert ("You are now leaving this site ");
Return true;
});
});

Note that. click () is just a simple method to implement. bind (). The following code is equivalent to the above implementation.Copy codeThe Code is as follows: $ (document). ready (function (){
$ ('A'). bind ('click', function (){
Alert ("You are now leaving this site ");
Return true;
});
});

But now you can add a link to the page through javascript.Copy codeThe Code is as follows: $ ('body'). append ('<div> <a href = "..."> Check it out! </A> </div> ');

However, when the user clicks the link, the method will not be called, because the link does not exist when you bind the click event to all <a> nodes on the page, so we use it. live () replacement. bind ():Copy codeThe Code is as follows: $ (document). ready (function (){
$ ('A'). live ('click', function (){
Alert ("You are now leaving this site ");
Return true;
});
});

Now, if you add a new link to the page, the binding can also run.

How. live () Works
. The magic behind live () is that it does not bind events to the selected elements, but is actually bound to the nodes of the DOM tree (in this example, $ (document )), instead, it is passed in element just like a parameter.

When you click an element, the click event will be passed up on the DOM tree until it reaches the root node. The trigger for this. click () event has been created by. live () on the root node. This trigger method first checks whether the clicked Target matches the selector called by. live. Therefore, in the above example, we will check whether the clicked element and $ ('A '). $ ('A') in live () matches. If yes, the binding method is executed.

No matter what you click in the root node, the. click () event of the root node will be triggered. This check will happen when you click any element added to the root node.

All. live () can be. die ()
If you know. bind (), you must know. unbind (). The relationship between. die () and. live () is similar. In order to contact the above binding (the dialog box is displayed when the user does not want to click the link), we do this:Copy codeThe Code is as follows: $ ('A'). die ();

More specifically, if other events are bound and need to be retained, such as hover or others, you can only unbind the click event.Copy codeThe Code is as follows: $ ('A'). die ('click ');

If you have defined a method name, you can unbind the specified method.Copy codeThe Code is as follows: specialAlert = function (){
Alert ("You are now leaving this site ");
Return true;
}
$ (Document). ready (function (){
$ ('A'). live ('click', specialAlert );
$ ('A'). live ('click', someOtherFunction );
});
// Then somewhere else, we cocould unbind only the first binding
$ ('A'). die ('click', specialAlert );

About. die ()
When using these functions, the. die () method has a disadvantage. Only the element Selector Used in the. live () method can be used. For example, it cannot be written as follows:Copy codeThe Code is as follows: $ (document). ready (function (){
$ ('A'). live ('click', function (){
Alert ("You are now leaving this site ");
Return true;
});
});
// It wowould be nice if we cowould then choose specific elements
// To unbind, but this will do nothing
$ ('A. no-alert '). die ();

. The die () event seems to have matched the target option and removed it. live () binding, but in fact, $ ('a. no-alert ') does not exist. Therefore, jquery does not find any binding to remove it.
Worse, the following:Copy codeThe Code is as follows: $ (document). ready (function (){
$ ('A, form'). live ('click', function (){
Alert ("You are going to a different page ");
Return true;
});
});
// NEITHER of these will work
$ ('A'). die ();
$ ('Form'). die ();
// ONLY this will work
$ ('A, form'). die ();

How to fix. die ()

In my next article, I will suggest a new method for. die () execution, which can provide a backward compatible tone. Maybe if I have time, I suggest the jQuery core development team accept my suggestions and modify them in the next release. I hope to add more methods I just wrote, including optional context parameters, nodes attached to custom events are allowed, rather than the root node.

For more information and examples, see the document of jQuery. live () and. die ().

Pay attention to. delegate () and. undelegate (). They can replace. live () and. die (). They are closely linked.

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.