jquery Event Repeat binding workaround

Source: Internet
Author: User

One $.fn.live repeat Binding

Workaround: Use the Die () method to dismiss the previously bound event on this element before the live () method binding, and then bind the new event through the Live () method.

// The Die () method is first removed, and then the $ ("#selectAll") is bound by Live (). Die (). Live ("Click",function() {/// event run Code });

Two Click and other events

Workaround: Use the Unbind ("click") method to unbind the event before binding the new event, that is, remove the original event on the object before binding the event to the object
Complete test Code:

<div class= "box" > <button id= "Test" > Duplicate binding Trigger button </button> (Click this button two times and above to trigger a duplicate binding, then click on the button below to see the result) <br/><br/> <button id= "test1" >click repeat binding Test button </button> <button id= " Test2 ">click bind once Test button </button> <button id=" test3 ">live repeat bind Test button </button> <butt On id= "Test4" >live bind once Test button </button> </div> <script type= "Text/javascript" src= "... /static/jquery-1.6.1.min.js "></script> <script type=" Text/javascript ">            $(function(){                vari = 1,j=1,k=1,h=1,n=1; varTriggerbind =function(){                    $("#test1"). Click (function() {alert ("Click does not unbind the duplicate binding to execute the" + j + + "Times");                    }); $("#test2"). Unbind (' click '). Click (function() {alert ("Click Unbind Execution" + k++ + "Times");                                        }); $("#test3"). Live ("click",function() {alert ("Live does not unbind repeat" + h++ + "Times");                    }); $("#test4"). Die (). Live ("click",function() {alert ("Live unbind and Execute" + n++ + "Times");                }); }                                $("#test"). Click (function() {triggerbind (); Alert ("Trigger binding Point hit" + i++ + "Times");            });        }); </script>

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.