jQuery上的“双击"事件(用于移动设备的dblclick) [英] jQuery on 'double click' event (dblclick for mobile)

查看:356
本文介绍了jQuery上的“双击"事件(用于移动设备的dblclick)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下jquery事件处理功能:

I have the following jquery event handling function:

$('.target').on('dblclick', function() {
    //respond to double click event
});

我的问题是此事件处理程序无法在触摸设备(iPhone,iPad ...)上运行.谁能推荐一个可靠的替代品dblclick,它可以在触摸设备上使用,并且仍然允许在全尺寸设备上舒适地双击使用?

My issue is that this event handler doesn't work on touch devices (iPhone, iPad...). Can anyone recommend a reliable alternative to dblclick that works on touch devices and still allows comfortable double click use on full size devices?

推荐答案

我最终建立了一个自定义的双击功能,该功能将在移动设备和台式机上都可以使用:

I ended up building a custom double click function that will work on both mobile and desktop:

var touchtime = 0;
$(".target").on("click", function() {
    if (touchtime == 0) {
        // set first click
        touchtime = new Date().getTime();
    } else {
        // compare first click to this click and see if they occurred within double click threshold
        if (((new Date().getTime()) - touchtime) < 800) {
            // double click occurred
            alert("double clicked");
            touchtime = 0;
        } else {
            // not a double click so set as a new first click
            touchtime = new Date().getTime();
        }
    }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="target">Double click me</div>

或者,这是 JSfiddle演示.

这篇关于jQuery上的“双击"事件(用于移动设备的dblclick)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆