单击并双击事件触发 [英] Click and double-click event firing

查看:98
本文介绍了单击并双击事件触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery编写事件处理程序,并且有关于点击和双击事件的问题。我有一个包含类文件的元素。它附有两个事件处理程序,一个用于单击元素,另一个用于双击。我希望两者都能独立于其他方式,但是当双击事件发生时,点击事件总是触发。

  $('。file')。live('dblclick',function(// event handler code){}); 
$('。file')。live('click',function(// event handler code){});

当用户双击事件附加到的项目时,双击事件应该火,这是正常的。但是,似乎任何单击事件都会触发。在jQuery中,如果双击事件触发,这是否意味着单击事件一直在同一个元素上触发?

解决方案

正如詹姆斯在评论中提到的那样,不可能没有其他的。这将是一个黑客,但你可以通过一个计时器独立工作:

  $('。file ').live('dblclick',function(){


$(this).data('doubleClicked',true);
//事件处理程序代码

});

函数SingleClickHandle(this)
{
if(!$(this).data('doubleClicked'))
{
//事件处理程序代码
}
}

$('。file')。live('click',function(){

$(this)数据('doubleClicked',false);
setTimeout('SingleClickHandler(this)',500); //是半秒钟,足以考虑它双击?

}) ;


I'm writing event handlers using jQuery, and had a question about the click and double-click events. I've got an element with a class "file" on it. It has two event handlers attached to it, one for single-clicking on the element, the other for double-clicking. I wanted both to fire independently of the other, but it appears the click event always fires when the double-click event does.

$('.file').live('dblclick', function(//event handler code){});
$('.file').live('click', function(//event handler code){});

When a user double-clicks an item with an event attached to it, a double-click event should fire, which is normal. However, it appears any single-click events are firing as well. In jQuery, if a double-click event fires, does that mean a single-click event has always already fired on the same element?

解决方案

As James mentions in his comment, there cannot be one without the other. It's going to be a bit of a hack, but you can have these work independently with a timer:

$('.file').live('dblclick', function() {


  $(this).data('doubleClicked', true);
  //event handler code

});

function SingleClickHandle(this)
{
  if (! $(this).data('doubleClicked'))
  {
  //event handler code
  }
}

$('.file').live('click', function(){

  $(this).data('doubleClicked', false);
  setTimeout('SingleClickHandler(this)', 500); // is half a second enough to consider it a double-click?

});

这篇关于单击并双击事件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆