在JavaScript中,是否有可能将双击视为单击?

3

我正在学习JavaScript。 我注意到,如果我快速地多次点击一个对象,某些单击将被捕获为双击。在JavaScript中是否有可能将所有点击都捕获为单击?


3
关注 mouseDown 事件而不是 click 事件。 - Kieren Johnstone
在IE浏览器上,如果我快速双击鼠标左键,事件顺序为:onmousedown、onmouseup、onclick、onmouseup、ondblclick。 - fatcatz
我需要捕获:onmousedown、onmouseup、onclick、onmousedown、onmouseup、onclick。如果我禁用ondblclick,那么我会得到:onmousedown、onmouseup、onclick、onmouseup。缺少onmousedown。 - fatcatz
2个回答

3
使用jQuery,您可以在文档上创建一个双击事件处理程序,然后阻止默认行为:
$(document).dblclick(function(e) {
    e.preventDefault();
    alert('Handler for .dblclick() called and ignored.');
});

双击示例可查看结果。


我想先学习没有任何库的Javascript。 - fatcatz

0

解决这个问题有两个选项。

1)在双击事件上使用“return false;”语句。 例如:

<button id="your button id" onclick="yourfuntion()" ondblclick="return false;" >My Button</button>

2) 在您的主函数开头禁用按钮/对象,在函数结束时再次启用它。

示例:

<button id="your button id" onclick="yourfuntion()">My Button</button>

<script>

function yourfuntion() { document.getElementById("your button id").disabled = true; 
//your javascript code 

document.getElementById("your button id").disabled = false;}

</script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接