Break down jQuery's event operations on related controls

Source: Internet
Author: User

Today, I suddenly became interested in his events. I have encountered these events before, but I have not sorted them out. I just want to get them out today.

For control events, jQuery has provided a wide range of methods, including binding, binding, and triggering. This morning, ara' can see how it can be used by daguo.

It is very convenient to bind events to jQuery, including bind, live, And one. It also helps you to separate some common events, such as The onclick event of the Control. When binding an onclick event, you only need

 
 
  1. $("#testButton").click(function() {  
  2.     alert("I'm Test Button");  
  3. }); 

In this way, The onclick event is bound to the testButton and the alert statement is executed. We can also use $ ("# testButton"). click (); to trigger this onclick event. Everything is very OK. The above is a bit sb. Next let's take a look at the cancellation event. JQuery has the unbind method, specifically used to cancel the binding, that is, to cancel the event. In the preceding example, you should use: $ ("# testButton "). unbind ("click"); well, it looks very good. 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 take a look at the example below. We will find that javascript events are exactly the same as C # events, and event binding is superimposed + =) instead of overwriting.

 
 
  1. VarEat=Function(){
  2. Alert ("I want to eat ");
  3. }
  4.  
  5. VarPayMoney=Function(){
  6. Alert ("pay first ");
  7. }
  8.  
  9. JQuery (document). ready (function (){
  10. $ ("# TestButton"). click (Eat );
  11. $ ("# TestButton"). bind ("click", PayMoney );
  12. });

Through the above example, we found that "I want to eat" will pop up and "pay first" will pop up, indicating that it is bound through onclick + = fn. Let's modify the ready method:

 
 
  1. jQuery(document).ready(function() {  
  2.            $("#testButton").click(Eat);  
  3.            $("#testButton").unbind();  
  4.            $("#testButton").bind("click", PayMoney);  
  5.        }); 

Another error occurred. If you click the button this time, you will only execute PayMoney and Eat. If you put unbind () behind bind, this button will not work. But what if I want to remove the bound PayMoney method? In this case, we should write:

 
 
  1. jQuery(document).ready(function() {  
  2.           $("#testButton").click(Eat);  
  3.           $("#testButton").bind("click", PayMoney);  
  4.           $("#testButton").unbind("click", PayMoney);  
  5.       }); 

Hey, it's actually the same as bind, but next you'll see a bug I don't know.) Let's get a close-up experience.

 
 
  1. <input id="testButton" type="button" value="Test Button" onclick="Eat();" /> 
  2. <script type="text/javascript"> 
  3.         jQuery(document).ready(function() {  
  4.             $("#testButton").unbind("click", Eat);  
  5.             $("#testButton").unbind();  
  6.             $("#testButton").bind("click", PayMoney);  
  7.         });  
  8. </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? The reason for this is to look at the jQuery class library. I guess it only deletes the events bound with JQuery. What should we do at this time? Fortunately, jQuery has many methods, one of which is attr, which operates the attributes of Dom elements. We use attr to eliminate click events on input. $ ("# TestButton "). attr ("onclick", ""); in this way, you can clear the onclick event. Remember, because attr is an attribute of an element, you must write "onclick" instead of "click" here, because click is a simplified method encapsulated by jQuery. Okay, the binding is here. Let's make a scenario. You can remember: One day, Lao Ying, Lao Zhao, and Lao Chen went out for dinner. They were full and had enough drinks and were ready to pay. At this time

 
 
  1. :<Head> 
  2.  
  3. <Script Type="Text/javascript" Src=Jquery-1.2.6.min.js"></Script> 
  4.  
  5. <Script Type="Text/javascript"> 
  6.  
  7. VarPayMoney=Function(Name ){
  8. Alert (name + ": My treat today, I will pay ");
  9. }
  10.  
  11. JQuery (document). ready (function (){
  12. $ ("# JeffreyPay"). attr ("onclick ","");
  13. $ ("# JamesPay"). attr ("onclick ","");
  14.  
  15. $ ("# JeffreyPay"). click (function (){
  16. Alert (".... You cannot swipe your card here ");
  17. });
  18.  
  19. $ ("# JeffreyPay"). click (function (){
  20. PayMoney ("Chen da ");
  21. });
  22. $ ("# JamesPay"). bind ("click", function (){
  23. Alert (".... Forgot to bring your wallet ");
  24. });
  25. $ ("# JamesPay"). bind ("click", $ ("# DlyingPay"). attr ("onclick "));
  26. });
  27. </Script> 
  28.  
  29. </Head> 
  30. <Body> 
  31. <Input Id="JeffreyPay" Onclick="PayMoney ('zhao Shuai ');" Type="Button" Value="Lao Zhao pays" /> 
  32. <Input Id="JamesPay" Type="Button" Onclick="PayMoney ('olde ');" Value="Regular payment" /> 
  33. <Input Id="DlyingPay" Type="Button" Onclick="PayMoney ('chen da ');" Value="Old Chen pays" /> 
  34. </Body> 

The above content is original and should not be used in evil places. In fact, there are still many bugs in binding events. You can modify the above effect a little and you will know, such as automatic execution and binding failure.

  1. JQuery calls the WCF Service to pass JSON objects
  2. Several common methods required for learning jQuery
  3. Construct an ASP. NET Website using XML + XSLT + CSS + JQuery
  4. Use jQuery and PHP to build an Ajax-Driven Web Page
  5. Notes for calling WCF using jQuery

Related Article

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.