[英]Double click ambiguity in Jquery?
I am trying to set some text in window for single and double click in jquery
. 我试图在窗口中设置一些文本,
single and double click in jquery
。
But I am facing the ambiguity
for double click
. 但我面临
double click
的ambiguity
。
When trying to do double click
, first single click
function works and then double click
works. 尝试
double click
,首先single click
功能,然后double click
工作。
Similar question is here check here and it was very old. 类似的问题在这里检查 ,这是非常古老的。 Also it has some problem.
它也有一些问题。
Any new answer using the latest jquery version. 任何使用最新jquery版本的新答案。
Check the live demo here of my problem live demo 查看我的问题 现场演示的现场演示
This my code , 这是我的代码,
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<a id="press" href="http://jquery.com">Click here</a>
<div id="log"></div>
<script>
$("#press").click(function(event) {
event.preventDefault();
$("#log").text("I am single click !");
return false;
});
$("#press").dblclick(function(event) {
event.preventDefault();
$('#log').text("I am double click !");
return false;
});
</script>
</body>
</html>
Hope our stack users will help me. 希望我们的堆栈用户能帮助我。
The way around the problem is to use a timeout for the click event to give the user time to doubleclick. 解决问题的方法是使用click事件的超时来为用户提供双击的时间。
If the user clicks twice within a certain timeframe, it's a doubleclick, if the user only clicks once, it's a normal click: 如果用户在特定时间范围内点击两次,则为双击,如果用户只点击一次,则为正常点击:
$('#press').click(function (event) {
event.preventDefault();
if ( $(this).data('dblclicked') ) {
$('#log').text('No google today!');
clearTimeout( $(this).data('clicked') );
$(this).data('dblclicked', false);
}else{
var self = this;
$(this).data('dblclicked', true).data('clicked', setTimeout(function() {
$('#log').text('google !');
$(self).data('dblclicked', false);
},300));
}
});
I got the closest solution by , 我得到了最接近的解决方案 ,
<a id="press" href="javascript:void(0)" onclick="singleClick(event)"
ondblclick="doubleClick(event)">Click here</a>
<div id="log"></div>
My JavaScript will be , 我的JavaScript会是,
var timer;
var status = 1;
function singleClick(event) {
event.preventDefault();
status = 1;
timer = setTimeout(function() {
if (status == 1) {
alert("I am single click !");
document.getElementById("log").innerHTML ='I am single click !';
}
}, 500);
}
function doubleClick(event) {
event.preventDefault();
clearTimeout(timer);
status = 0;
alert("I am double click !");
document.getElementById("log").innerHTML = 'I am a double click!';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.