Home >Web Front-end >JS Tutorial >Quick solution to repeated binding of jquery events_jquery

Quick solution to repeated binding of jquery events_jquery

WBOY
WBOYOriginal
2016-05-16 17:05:25966browse

One $.fn.live duplicate binding

Solution: Use the die() method. Before binding the live() method, unbind all previously bound events on this element, and then pass the live () method binds new events.

Copy code The code is as follows:

//First release it through the die() method, and then pass it live() binding
$(“#selectAll”).die().live(“click”,function(){
//Event running code
});

Second click and other events

Solution: Use the unbind("click") method to unbind the bound event first and then bind the new event, that is, remove the object before binding the event to the object Original event on

Complete test code:

Copy code The code is as follows:

Trigger repeated binding, then click the button below to see the result)                                                                                                                                                                                                                                                                                             /button>
"../static/jquery-1.6.1.min.js">
                                                                                             var i = 1,j=1,k=1,h=1,n=1;
        var triggerBind = function(){
                                                                                                                           >                 alert("click is not unbinded and repeated binding is executed " j " times"); {
                       alert("click unbinding execution " k "times"); ALERT (" LIVE's unlimited binding and repeated execution of the "h" times ");
});
$ ("#test4 "). DIE (). {
                                                                                                                                                                                        $("#test").click(function() {
triggerBind (); 🎜>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn