异想天开

What's the true meaning of light, Could you tell me why

页面中鼠标单击与双击事件

日期:2015-07-16 20:04:47
  
最后更新日期:2015-07-16 20:05:33
也是在捣鼓站点后台时,发现网页鼠标双击会产生两个单击事件。若需要同时捕获单击和双击事件,可以这样: 判断双击是在600ms毫秒内,若有两次单击事件,那么为双击。用一个变量作一个状态机,判断是第一次单击还是第二次单击。用一个完整例子实现该需求:
[code lang="cpp"]
<html>
<head>
<title>测试鼠标单击和双击</title>
</head>
<body>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script>
<a href="http://www.baidu.com" target="_blank" id="test">百度</a>
<script type="text/javascript">
var click_state = 0;
var tmclock = null;
var $obj = null;
var g_count = 0;
$("#test").click(function(){
if (0 == click_state){
click_state = 1;
$obj = $(this);
tmclock = setTimeout(function(){
click_state = 0;
window.open($obj[0].href);
},600);
}
g_count = g_count + 1;
return false;
});
$("#test").dblclick(function(){
clearTimeout(tmclock);
click_state = 0;
window.alert(g_count);
g_count = 0;
return false;
});
</script>
</body>
</html>
[/code]