javascript鼠标三击事件 鼠标连续点击三次事件怎么实现啊?我觉得应该用setTimeout和计数器变量来写,但想了好久还是没想出思路来,不知道怎么弄出来,求解啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用一个变量var i =0;单击一次加1等于3时,发生事件,再i = 0; 先来个 双击的function ClickState(sgl, dbl) { var me = this, state = null, timer0 = null; function s0() { timer0 = setTimeout(function() { state = s0; sgl(); }, 200); state = s1; } function s1() { clearTimeout(timer0); state = s0; dbl(); } state = s0; this.next = function() { state(); };}function sgl() {console.info('single');}function dbl() {console.info('double');}state = new ClickState(sgl, dbl);document.onclick = function() { state.next();};关于上面的写法,参考 javascript 解释器模式: http://www.uml.org.cn/sjms/200805214.asp看懂双击再看三击就简单了function ClickState(sgl, dbl) { var me = this, delay = 200, state = null, timer = null; function s0() { timer = setTimeout(function() { state = s0; sgl(); }, delay); state = s1; } function s1() { clearTimeout(timer); timer = setTimeout(function() { state = s0; dbl(); }, delay); state = s2; } function s2() { clearTimeout(timer); timer = setTimeout(function() { state = s0; tpl(); }, 200); state = s2; } state = s0; this.next = function() { state(); };}function sgl() {console.info('single');}function dbl() {console.info('double');}function tpl() {console.info('triple');}state = new ClickState(sgl, dbl, tpl);document.onclick = function() { state.next();};四击之类也很容易扩展 写错,三击的定义是function ClickState(sgl, dbl, tpl) { 每击一次,改变一下变量,根据变量decide!!! document.onclick = function(){ if(count < 3){ if(timer){ clearTimeout(timer); } count ++; timer = setTimeout(function(){ count = 0; }, 500); }else if(count = 3){ count = 0; alert('three click'); } } 上面少复制了一行。。 var count = 0, timer; document.onclick = function(){ if(count < 3){ if(timer){ clearTimeout(timer); } count ++; timer = setTimeout(function(){ count = 0; }, 500); }else if(count = 3){ count = 0; alert('three click'); } } 好吧,上面的又写错了。。 var count = 0, timer; document.onclick = function(){ if(count < 2){ if(timer){ clearTimeout(timer); } count ++; timer = setTimeout(function(){ count = 0; }, 500); }else if(count === 2){ count = 0; clearTimeout(timer); threeClick(); } } function threeClick(){ alert('three click'); } 写着玩儿,LZ可以试试<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript"> </script><style type="text/css"></style></head><body><input type="text" id='a' onclick='' value='三点'/><div style="height:1000px;width:400px;">sdf</div><script type="text/javascript"> function ck(n, fn) { this.fn = fn; this.size = n; this.index = 0; this.flag = false; this.id = null; } ck.prototype = { action: function() { this.flag = true; this.index++; if(this.index === this.size) { this.reset(); s = []; Array.prototype.push.apply(s, arguments); this.fn.apply(s.shift(), s); } else { this.id = setTimeout((function(o, n) { return function() { o.time(n); } })(this, this.index), 200); } }, time: function(n) { if(n >= this.index) { this.reset(); } }, reset: function() { this.flag = false; this.index = 0; clearTimeout(this.id); } }; var cl = new ck(3, function(e, str) { alert(this.value); alert(e.button); alert(str); }); document.getElementById('a').onclick = function(e){ cl.action(this, e, 'dddd') };</script></body></html> 写程序不能太实际了你们要看清楚大于 1击的 就代表 N击事件(N表示一个 >1的整数)剩下的去实现吧 一个关于google maps api的标注的问题 jquery 与datalist 事件绑定? 怎么会提示参数无效 ExtJS 格式化日期 JS和表单提交的关联问题(文件已打包)--在线等 jQuery slideDown slideUp 怎么用啊 知道 梅花雪 的qq喽~~~~~~~~~~~~~~~~~~~~~~ 下拉列表与层 高手帮忙,关于JS文本框,谢谢 怎样兼容浏览器:这段代码为什么不运行了? 纯JS做俄罗斯方块的问题 黄金实时行情、外汇实时行情、沪深实时行情应用
单击一次加1
等于3时,发生事件,再i = 0;
function ClickState(sgl, dbl) {
var me = this,
state = null,
timer0 = null; function s0() {
timer0 = setTimeout(function() {
state = s0;
sgl();
}, 200);
state = s1;
}
function s1() {
clearTimeout(timer0);
state = s0; dbl();
}
state = s0; this.next = function() {
state();
};
}function sgl() {console.info('single');}
function dbl() {console.info('double');}state = new ClickState(sgl, dbl);
document.onclick = function() {
state.next();
};
关于上面的写法,参考 javascript 解释器模式: http://www.uml.org.cn/sjms/200805214.asp看懂双击再看三击就简单了function ClickState(sgl, dbl) {
var me = this,
delay = 200,
state = null,
timer = null; function s0() {
timer = setTimeout(function() {
state = s0;
sgl();
}, delay);
state = s1;
}
function s1() {
clearTimeout(timer);
timer = setTimeout(function() {
state = s0;
dbl();
}, delay);
state = s2;
}
function s2() {
clearTimeout(timer);
timer = setTimeout(function() {
state = s0;
tpl();
}, 200);
state = s2;
}
state = s0; this.next = function() {
state();
};
}function sgl() {console.info('single');}
function dbl() {console.info('double');}
function tpl() {console.info('triple');}state = new ClickState(sgl, dbl, tpl);
document.onclick = function() {
state.next();
};四击之类也很容易扩展
function ClickState(sgl, dbl, tpl) {
if(count < 3){
if(timer){
clearTimeout(timer);
}
count ++;
timer = setTimeout(function(){
count = 0;
}, 500);
}else if(count = 3){
count = 0;
alert('three click');
}
}
document.onclick = function(){
if(count < 3){
if(timer){
clearTimeout(timer);
}
count ++;
timer = setTimeout(function(){
count = 0;
}, 500);
}else if(count = 3){
count = 0;
alert('three click');
}
}
document.onclick = function(){
if(count < 2){
if(timer){
clearTimeout(timer);
}
count ++;
timer = setTimeout(function(){
count = 0;
}, 500);
}else if(count === 2){
count = 0;
clearTimeout(timer);
threeClick();
}
}
function threeClick(){
alert('three click');
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
</script>
<style type="text/css"></style>
</head>
<body>
<input type="text" id='a' onclick='' value='三点'/>
<div style="height:1000px;width:400px;">sdf</div>
<script type="text/javascript">
function ck(n, fn) {
this.fn = fn;
this.size = n;
this.index = 0;
this.flag = false;
this.id = null;
}
ck.prototype = {
action: function() {
this.flag = true;
this.index++;
if(this.index === this.size) {
this.reset();
s = [];
Array.prototype.push.apply(s, arguments);
this.fn.apply(s.shift(), s);
} else {
this.id = setTimeout((function(o, n) {
return function() {
o.time(n);
}
})(this, this.index), 200);
}
},
time: function(n) {
if(n >= this.index) {
this.reset();
}
},
reset: function() {
this.flag = false;
this.index = 0;
clearTimeout(this.id);
}
};
var cl = new ck(3, function(e, str) {
alert(this.value);
alert(e.button);
alert(str);
});
document.getElementById('a').onclick = function(e){
cl.action(this, e, 'dddd')
};
</script>
</body>
</html>
大于 1击的 就代表 N击事件(N表示一个 >1的整数)剩下的去实现吧