onClick和onDblClick并存的两种解决方法

一、

var v_Result;
function OneClick(event) {
    console.log("detail",event.detail);
    //if (event.detail == 2) 
    //	return ; 
    v_Result = false;
    window.setTimeout(check, 300);
    function check() {
        if (v_Result != false) return;
        console.log("单击");
    }
}
function TwoClick() {
    v_Result = true;
    console.log("双击");
}
<input type="button" οnclick="OneClick(event)" οndblclick="TwoClick()" value="点我">

二、

var clickTimer = null;
function _click() {
    if (clickTimer) {
        console.log("clearTimeout", clickTimer);
        window.clearTimeout(clickTimer);
        clickTimer = null;
    }

    clickTimer = window.setTimeout(function() {
        // your click process code here
        console.log("你单击了我");
    },
    300);
    console.log("setTimeout", clickTimer);
}

function _dblclick() {
    console.log("dblclick");
    if (clickTimer) {
        console.log("=clearTimeout", clickTimer);
        window.clearTimeout(clickTimer);
        clickTimer = null;
    }

    // your click process code here
    console.log("你双击了我");
}
<button οnclick="_click();" οndblclick="_dblclick();">单击或双击我</button>

另外,根据event.detail可以获知是单击还是双击,单击detail值是1,双击值是2。但是双击时,OneClick(event)会被调用两次,第一次detail值是1,第二次值是2

参考:1、http://my.oschina.net/jsan/blog/123181

2、http://smartwang.blog.51cto.com/3270660/1415400


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值