单击并双击事件触发 [英] Click and double-click event firing
问题描述
$('。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屋!