Jquery's unbind () function details

Source: Internet
Author: User

Turn: http://www.popo4j.com/article/jQuery-unbind.html

Jquery's binding events are very convenient, including BIND, unbind, live, And one. It also helps you separate some common events, such as the onclick event of the control, when binding an onclick event, you only need

 
$ ("# Testbutton"). Click (Function(){
Alert ("I'm test button");
});

What if we want to cancel the binding event?
Jquery has the unbind method, which is used to cancel binding events.

$ ("# Testbutton"). Unbind ("Click");

Is it easy? If you have two click events, you can also use unbind ("click", fnname) to delete the binding of a specific function.

Why is there a way to cancel a specific function? Let's look at the example below and we will find that,JavascriptSame as C # events, event binding is superimposed (+ =) rather than overwriting.

 VaR  Eat  =     Function  (){
Alert ( " I want to eat " );
}
VaR Paymoney = Function (){
Alert ( " Pay first " );
}
Jquery (document). Ready ( Function (){
$ ( " # Testbutton " ). Click (EAT );
$ ( " # Testbutton " ). BIND ( " Click " , Paymoney );
});

We found that "I want to eat" will pop up first, and then "pay first" will pop up"

To unbind a specific function:

$ ("# Testbutton"). Unbind ("Click", Paymoney );

Note: The jquery unbind method can only cancel events bound to the jquery BIND () method,

In the jquery source code, we can see $ (). click to call $ (). BIND ("click") event, so as long as it is an event added by jquery, unbind can unbind the event. unbind is powerless for such events added by native JS or events added by other JS frameworks.

 <  Input ID  =  "  Testbutton  "  Type =  "  Button  "  Value  =  "  Test button  "  Onclick  =  "  Eat ();  "     /  >  
< Script Type = " Text/JavaScript " >
Jquery (document). Ready ( Function (){
$ ( " # Testbutton " ). Unbind ( " Click " , Eat );
$ ( " # Testbutton " ). Unbind ();
$ ( " # Testbutton " ). BIND ( " Click " , Paymoney );
});
< / SCRIPT>

Let's guess what it will show? Dinner? Pay? The answer is eat-> paymoney, ah !!! I canceled the binding and deleted the specific binding. Why is the Eat still executed?

Take a closer look.

 
Onclick="Eat ();"

How does the unbind function solve this problem? Let's take a look at this article.ArticleJquery replaces the events bound to the element.

 

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.