简体   繁体   English

当我 dblclick 时,它会运行 .click 两次

[英]When I dblclick, it runs .click twice

I have 2 functions when you click on an item, .click and .dblclick The problem is, that when you dblclick, it runs the .click function twice.当您单击一个项目时,我有 2 个函数,.click 和 .dblclick 问题是,当您单击 dblclick 时,它会运行 .click 函数两次。 Is there a way to fix this?有没有办法解决这个问题?

$(".item").click(function() {
    if (price[this.id] === 1) {
        consoleUpdate("This item is worth " +price[this.id]+ " coin");
    }
}

$("#fishItem").unbind("dblclick").bind("dblclick").dblclick(function() {
    coins = coins+1;
    coinUpdate();
    invFish1--;
    if (invFish1 === 0) {
        $("#fishItem").appendTo("#itemStage");
    }
});   

Per the jQuery documentation for the dblclick event:根据 dblclick 事件的 jQuery 文档:

It is inadvisable to bind handlers to both the click and dblclick events for the same element.不建议将处理程序绑定到同一元素的 click 和 dblclick 事件。 The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one.触发事件的顺序因浏览器而异,有些在 dblclick 之前收到两个点击事件,而其他只收到一个。 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.双击灵敏度(被检测为双击的最大单击间隔时间)可能因操作系统和浏览器而异,并且通常是用户可配置的。

You want to change your events so that you don't have a .click and .dblclick on the same element.你想改变你的活动,让你没有.click.dblclick在相同的元素。

http://api.jquery.com/dblclick/ http://api.jquery.com/dblclick/

var cnt=1;
$( "#target" ).click(function(e) {

if(cnt==1)
{

// action on single click if you dont want anything in in single click then use here
e.preventDefault();

}else{

// work on double click
cnt=1;// again set counter 
}

cnt++;
});

reference e.preventDefault();参考e.preventDefault();

As others mentioned, you can't really bind both click and double click to the same item.正如其他人提到的,您不能真正将单击和双击绑定到同一个项目。 There are, however, workarounds.但是,有一些解决方法。

One way to handle this would be to handle the click code and then check for double-click:处理此问题的一种方法是处理单击代码,然后检查双击:

var lastClickTime = 0;
$(".item").click(function() {
    var thisClickTime = (new Date).getTime();

    if (thisClickTime - lastClickTime < 500) {
        //Double-click
    }
    else {
        //Click
    }
    lastClickTime = thisClickTime;
});

This code essentially captures a "last click time" and if this click time is less than 500 milliseconds from the last click time, then it fires the double-click code.这段代码本质上捕获了“最后一次点击时间”,如果这个点击时间距离最后一次点击时间小于 500 毫秒,那么它会触发双击代码。 Otherwise it will fire the click code.否则它会触发点击代码。

The downside with this is that the click-code will be called first and then the double-click code.这样做的缺点是点击代码将首先被调用,然后是双击代码。 Also, you're forcing your users into a 500ms double-click.此外,您还强迫您的用户进行 500 毫秒的双击。 While this is pretty standard, it's not guaranteed.虽然这是非常标准的,但不能保证。 (Slower clickers may have trouble with this.) (较慢的答题器可能会遇到这个问题。)

A JSFiddle that demonstrates this technique.演示此技术的JSFiddle


You could improve this code by setting a 500ms timeout for the click code and then cancelling the click event if a double-click is fired.您可以通过为单击代码设置 500 毫秒超时,然后在触发双击时取消单击事件来改进此代码。 The downside with this is that it will force a 500 ms delay between the click and the "click" code.这样做的缺点是它会在点击和“点击”代码之间强制延迟 500 毫秒。

A JSFiddle that demonstrates the timeout.演示超时的JSFiddle

If you don't mind handling one single-click, look at the event object details attribute.如果您不介意处理一次单击,请查看事件对象详细信息属性。 It is the number of clicks.这是点击次数。

$(".item").click(function(event) {
    if (event.details === 1) {
        //handle single click
    } else if (event.details === 2) {
        // handle double click
    }
}

Juan's answer is a great workaround, however I believe it should be event.detail (singular). Juan 的回答是一个很好的解决方法,但我认为它应该是event.detail (单数)。

UIEvent.detail:https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail UIEvent.detail:https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM