Home > Web Front-end > JS Tutorial > jquery click event and double-click event conflict resolution_jquery

jquery click event and double-click event conflict resolution_jquery

WBOY
Release: 2016-05-16 15:12:26
Original
1665 people have browsed it

I need to add a double-click event to the tree node of bootstrap-treeview. The plug-in's native method does not have a double-click event function. The plug-in's node is bound to the click event by default, which causes a conflict between the click event and the double-click event.

Write test code

Conflicting code:

Problem effect display:

Every time a double-click event is triggered, it will cause two click events

Code to resolve conflicts:

Problem-solving effect display:

Perfectly solve the conflict problem between click event and double-click event

Here we mainly use two functions in the HTMLDOMWindow object, settimeout(), clearTimeout()

I set the time interval between the two click events to be triggered at 300 milliseconds, which needs to be determined based on the actual situation.

The source code is as follows:

<!DOCTYPE html>
<html>

 <head>
  <meta charset="utf-8">
  <title></title>
 </head>

 <body>
  <div>事件监控</div>
 </body>
 <script type="text/javascript" src="js/jquery.min.js"></script>
 <script type="text/javascript">
  /*$(function() {
     $("div").bind("click.a", function() { //单击事件 
      $("body").append("<p>click事件</p>");
     })
     $("div").bind("dblclick.a", function() { //双击事件 
      $("body").append("<p>dblclick事件</p>");
     })
     $("div").bind("mouseover.a", function() { //鼠标经过元素的事件 
      $("body").append("<p>mouseover事件</p>");
     })
     $("div").bind("mouseout.a", function() { //鼠标移出元素的事件 
      $("body").append("<p>mouseout事件</p>");
     })
    })*/
  $(function() {
   var timer = null;
   $("div").bind("click.a", function() { //单击事件 
    clearTimeout(timer);
    timer = setTimeout(function() { //在单击事件中添加一个setTimeout()函数,设置单击事件触发的时间间隔 
     $("body").append("<p>click事件</p>");
    }, 300);
   })
   $("div").bind("dblclick.a", function() { //双击事件 
    clearTimeout(timer); //在双击事件中,先清除前面click事件的时间处理 
    $("body").append("<p>dblclick事件</p>");
   })
   $("div").bind("mouseover.a", function() { //鼠标经过元素的事件 
    $("body").append("<p>mouseover事件</p>");
   })
   $("div").bind("mouseout.a", function() { //鼠标移出元素的事件 
    $("body").append("<p>mouseout事件</p>");
   })
  })
 </script>

</html>
Copy after login

The above is the jquery click and double-click event conflict resolution, I hope it will be helpful to everyone's learning.

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