Home >Web Front-end >JS Tutorial >jQuery cancel specific click event_jquery
The example in this article describes the jQuery implementation method of canceling a specific click event. Share it with everyone for your reference, the details are as follows:
As we all know, jQuery can bind the same event multiple times, and each bound event can be executed. Here comes the problem. In the dynamically generated DOM, we bind two different clicks (assumed to be A and B) for a certain element. When appending the element, all elements are bound to B again... This will lead to the final When clicked, the B event will double up.
Fortunately, jQuery provides us with a very elegant way to cancel clicks under a specific namespace.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>无标题页</title> <script src="jquery/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ $("#divTest").click(function(){ alert("正式事件。"); }); }); function bindEvent(){ for(var i=0;i<3;i++){ $("#divTest").bind("click.test",function(){ testEvent(); }); } } function testEvent(){ alert("测试事件"); } function ignoreMultiEvent(){ $("#divTest").unbind("click.test").bind("click.test",function(){ testEvent(); }); } </script> </head> <body> <div id="divTest" style="height: 163px;text-align:center;line-height:163px;width: 500px; background-color: #0000FF;"> 点我进行测试 </div> <input id="Button2" type="button" value="为上面的DIV绑定3次测试事件" onclick="bindEvent()" /> <input id="Button1" type="button" value="保留正式事件, 取消已绑定的多次测试事件,再绑定一次测试事件 " onclick="ignoreMultiEvent()" /> </body> </html>
Readers who are interested in more jQuery-related content can check out the special topics on this site: "JQuery drag effects and skills summary", "jQuery extension skills summary", "JQuery common classic special effects summary", "jQuery animation and special effects usage summary", "jquery selector usage summary" and "jQuery common plug-ins and usage Summary》
I hope this article will be helpful to everyone in jQuery programming.