jQuery cancel specific click event
May 16, 2016 pm 03:57 PMThis article mainly introduces jQuery's method of canceling specific click events, and analyzes the related techniques of jQuery to simply implement event binding and cancel event binding in the form of examples. Friends in need can refer to it
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(){ $("#pTest").click(function(){ alert("正式事件。"); }); }); function bindEvent(){ for(var i=0;i<3;i++){ $("#pTest").bind("click.test",function(){ testEvent(); }); } } function testEvent(){ alert("测试事件"); } function ignoreMultiEvent(){ $("#pTest").unbind("click.test").bind("click.test",function(){ testEvent(); }); } </script> </head> <body> <p id="pTest" style="height: 163px;text-align:center;line-height:163px;width: 500px; background-color: #0000FF;"> 点我进行测试 </p> <input id="Button2" type="button" value="为上面的p绑定3次测试事件" onclick="bindEvent()" /> <input id="Button1" type="button" value="保留正式事件, 取消已绑定的多次测试事件,再绑定一次测试事件 " onclick="ignoreMultiEvent()" /> </body> </html>
For more related tutorials, please visit JavaScript video tutorial

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags

In-depth analysis: jQuery's advantages and disadvantages

Understand the role and application scenarios of eq in jQuery

How to tell if a jQuery element has a specific attribute?
