如何在 jQuery 中监听单击并按住? [英] How can I listen for a click-and-hold in jQuery?

查看:25
本文介绍了如何在 jQuery 中监听单击并按住?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在用户单击按钮时触发事件,然后按住该单击 1000 到 1500 毫秒.

I want to be able to fire an event when a user clicks on a button, then holds that click down for 1000 to 1500 ms.

是否有 jQuery 核心功能或插件已经支持此功能?

Is there jQuery core functionality or a plugin that already enables this?

我应该自己动手吗?我应该从哪里开始?

Should I roll my own? Where should I start?

推荐答案

var timeoutId = 0;

$('#myElement').on('mousedown', function() {
    timeoutId = setTimeout(myFunction, 1000);
}).on('mouseup mouseleave', function() {
    clearTimeout(timeoutId);
});

编辑:根据 AndyE 进行更正...谢谢!

Edit: correction per AndyE...thanks!

编辑 2:现在对每个 gnarf 具有相同处理程序的两个事件使用绑定

Edit 2: using bind now for two events with same handler per gnarf

这篇关于如何在 jQuery 中监听单击并按住?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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