One $. fn. live repeat binding
Solution:Use the die () method to unbind all events bound to the element before binding the live () method, and then bind new events using the live () method.
Copy codeThe Code is as follows:
// Unbind from the die () method and then use live () Binding
$ ("# SelectAll"). die (). live ("click", function (){
// Event Running code
});
Second click and other events
Solution:The unbind ("click") method is used to unbind the Bound event and then bind the new event. That is, the original event on the object is removed before the event is bound to the object.
Complete Test code:
Copy codeThe Code is as follows:
<Div class = "box">
<Button id = "test"> repeat binding trigger button </button> (click this button twice or more to trigger repeat binding, and then click the following button to see the result)
<Br/>
<Button id = "test1"> click to bind the test button again </button>
<Button id = "test2"> click to bind a test button </button>
<Button id = "test3"> repeat the test button in live </button>
<Button id = "test4"> live binds a test button </button>
</Div>
<Script type = "text/javascript" src = "../static/jquery-1.6.1.min.js"> </script>
<Script type = "text/javascript">
$ (Function (){
Var I = 1, j = 1, k = 1, h = 1, n = 1;
Var triggerBind = function (){
$ ("# Test1"). click (function (){
Alert ("click is not unbound and rebinding is executed for" + j ++ "times ");
});
$ ("# Test2"). unbind ('click'). click (function (){
Alert ("click unbind execution" + k ++ "times ");
});
$ ("# Test3"). live ("click", function (){
Alert ("live is not unbound and repeat execution No." + h ++ "times ");
});
$ ("# Test4"). die (). live ("click", function (){
Alert ("executed after live is unbound" + n ++ "times ");
});
}
$ ("# Test"). click (function (){
TriggerBind ();
Alert ("trigger binding click" + I ++ "times ");
});
});
</Script>