点击时被doubleclick停止 [英] on click stopped by doubleclick

查看:79
本文介绍了点击时被doubleclick停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery中,我有一个单击即可执行的函数,但在双击时会执行另一个函数,问题是当我双击它时,它会单击2次并双击两次,因此3次执行... >

这是我的代码:

$('.listCat li').on({
    click: function() {
        $(this).toggleClass("on");
        refresh();
    },
    dblclick: function() {
        $('.listCat li.on').removeClass('on');
        $(this).addClass("on");
        refresh();
    }
});

我当时想在点击时稍加延迟,然后检测是否有双击,否则请启动refresh().

您认为这样做的一种好方法吗?

谢谢.

解决方案

jQuery .dblclick的文档建议您不要同时使用它们:

不建议将处理程序绑定到同一元素的click和dblclick事件.触发事件的顺序因浏览器而异,其中一些事件在dblclick之前收到两个click事件,其他事件仅收到一个.双击敏感度(被检测为两次单击的最大间隔时间)会因操作系统和浏览器而异,并且通常可由用户配置.

解决方法

如果您必须这样做,请 jQuery:当`dblclick`时,抑制`click`.迫在眉睫?

In jQuery I got a function that executes on a click, but another on a double click, the problem is that when I double click on it it clicks 2 times and do a double click so 3 executions...

here is my code :

$('.listCat li').on({
    click: function() {
        $(this).toggleClass("on");
        refresh();
    },
    dblclick: function() {
        $('.listCat li.on').removeClass('on');
        $(this).addClass("on");
        refresh();
    }
});

I was thinking in the click to make a small delay and detects if there is a doubleclick, otherwise launch refresh().

Do you think of a nice clean way to do that ?

Thank you.

解决方案

The docs for jQuery .dblclick suggest that you don't use them together:

It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable.

Workaround

If you must do this, there is a workaround on this question (as pavan mentioned in a comment on your question). I wouldn't completely trust it, though. As the jQuery docs say, the length of a double-click is often user configurable.

Also see this question: jQuery: Suppress `click` when `dblclick` is imminent?

这篇关于点击时被doubleclick停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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