Click nesting may occur frequently in projects. If you do not pay attention to it, it is easy to execute repeatedly, as follows: & amp; lt; script & amp; gt ;$ (function () {var
Click nesting may occur frequently in projects. If you do not pay attention to it, repeated execution may easily occur, as shown below:
Script $ (function () {var num = 1; $ ("# test "). click (function () {$ ("# test "). click (function () {alert ("Number" + num ++ "click execution ");}); alert ("Number" + num ++ "click execution") ;}) script click test
There are multiple solutions:
(1) Use unbind ("click") to unbind the event and then bind the new event.
Script $ (function () {$ ("# test "). click (function () {$ ("# test "). unbind ('click '). click (function () {alert ("Internal click execution") ;}); alert ("external click execution") ;}) script
(2) Use die () to remove all bound events before live ().
Script $ (function () {$ ("# test "). die (). live ("click", function () {$ ("# test "). die (). live ("click", function () {alert ("Internal click execution") ;}); alert ("external click execution") ;});}) script