jQuery中有什么东西可以让我区分双击和单击的行为吗?
When I bind both to same element only the single click gets executed.
是否有一种方法可以在执行单击之前等待一段时间,以查看用户是否再次单击?
谢谢:)
jQuery中有什么东西可以让我区分双击和单击的行为吗?
When I bind both to same element only the single click gets executed.
是否有一种方法可以在执行单击之前等待一段时间,以查看用户是否再次单击?
谢谢:)
I found that John Strickler's answer did not quite do what I was expecting. Once the alert is triggered by a second click within the two-second window, every subsequent click triggers another alert until you wait two seconds before clicking again. So with John's code, a triple click acts as two double clicks where I would expect it to act like a double click followed by a single click.
我修改了他的解决方案,以便以这种方式发挥作用,并以我的大脑可以更好地理解的方式流动.我把延迟从2000降到了700,以便更好地模拟我感觉到的正常灵敏度.这里是小提琴:http://jsfiddle.net/KpCwN/4/.
谢谢你的基金会,约翰.我希望这个备选版本对其他人有用.
var DELAY = 700, clicks = 0, timer = null;
$(function(){
$("a").on("click", function(e){
clicks++; //count clicks
if(clicks === 1) {
timer = setTimeout(function() {
alert("Single Click"); //perform single-click action
clicks = 0; //after action performed, reset counter
}, DELAY);
} else {
clearTimeout(timer); //prevent single-click action
alert("Double Click"); //perform double-click action
clicks = 0; //after action performed, reset counter
}
})
.on("dblclick", function(e){
e.preventDefault(); //cancel system double-click event
});
});