Home > Web Front-end > JS Tutorial > Example analysis of how to cancel subsequent execution content in jQuery

Example analysis of how to cancel subsequent execution content in jQuery

高洛峰
Release: 2016-12-03 09:13:17
Original
1014 people have browsed it

This article mainly introduces examples of canceling subsequent execution content in jQuery, and the code is easy to understand. Friends who need it, please take a look

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title></title>
 <script type="text/javascript">
  //点击a标签,不进行页面跳转
  window.onload = function () {
   var obj = document.getElementById("myhref");
   obj.onclick = function (event) {
    //取消默认行为
    //return false;
    //分浏览器
    //IE下
    //01.第一道能力检测
    event = event || window.event;
    if (event.preventDefault) {
 
      //非IE下
     event.preventDefault();
    } else {
     event.returnValue = false;
    }    
   };
  }
 </script>
</head>
<body>
 <a id="myhref" href="http://www.baidu.com">去百度</a>
</body>
</html>
Copy after login

The click effect of the a tag here is originally to jump to the Baidu page, but we can cancel the default behavior through parameters so that the subsequent content of this click event will not be executed.

Sometimes there are multiple events after clicking on the same label. If you only want to execute the first event and abandon the subsequent events, you can add a piece of code to prevent it:

click me!

How does jquery prevent post-bound events

Your code has already been loaded during the page loading process After the event binding is completed, there is no way to prevent the later-bound events, but you can delete the event binding of the currently specified node. The method is as follows:

$("#btn").click(function(){
if($("#tx").val()==""){
alert("e1");
}else{
//删除后绑定的事件。。。
$("#btn").unbind(&#39;click&#39;);
}
});
Copy after login

Description: The reverse operation of

unbind([type],[data])
bind() removes the bound event from each matching element.

If there are no parameters, all bound events will be deleted.

You can unbind the custom event you registered with bind().

I If an event type is provided as a parameter, only bound events of that type will be removed.

If the handler function passed when binding is used as the second parameter, only this specific event handler function will be deleted.

jquery: How to prevent the second click event?

jquery provides a method to trigger only one click

obj.one(function(){
});
Copy after login

or use obj.unbind("click") to cancel the click event

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template