javascript Backbone.js - 单击和双击事件都在元素上触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13286323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:18:31  来源:igfitidea点击:

Backbone.js - Both click and double click event getting fired on an element

javascriptjavascript-eventsbackbone.js

提问by Veera

In my Backbone View, I have defined events like this:

在我的主干视图中,我定义了这样的事件:

events : {
  'click .elm' : 'select',
  'dblclick .elm' : 'toggle'

},

select: function(e){
    e.preventDefault();
    console.log('single clicked');
}

toggle : function(e){
    e.preventDefault();
    console.log('double clicked');
}

I have bound single and double click event listeners to same element .elm. When I double click on this element, I get this output:

我已将单击和双击事件侦听器绑定到同一元素.elm。当我双击这个元素时,我得到这个输出:

single clickedsingle clickeddouble clicked

single clickedsingle clickeddouble clicked

Tried preventDefault()and stopImmediatePropagation()and that didn't solve the issue.

尝试preventDefault()stopImmediatePropagation()并没有解决不了的问题。

So, how can I prevent the single click event getting fired when I double click?

那么,如何防止在双击时触发单击事件?

采纳答案by Pramod

This isn't an issue with Backbone itself. It's about how to handle both single click and double click events on the same button.

这不是 Backbone 本身的问题。它是关于如何处理同一个按钮上的单击和双击事件。

See

  1. Need to cancel click/mouseup events when double-click event detected

  2. Javascript with jQuery: Click and double click on same element, different effect, one disables the other

  1. 检测到双击事件时需要取消单击/鼠标向上事件

  2. 带有 jQ​​uery 的 Javascript:单击并双击同一元素,效果不同,一个禁用另一个

Update: But it would be better if you didn't have to deal with it. See mu is too short's answerbelow!

更新:但是如果您不必处理它会更好。看下面的mu 太短的回答

回答by mu is too short

The jQuery documentationspecifically recommends against what you're doing:

jQuery文档特别建议对你在做什么:

It is inadvisable to bind handlers to both the clickand dblclickevents for the same element. The sequence of events triggered varies from browser to browser, with some receiving two clickevents before the dblclickand 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.

不建议将处理程序绑定到同一元素的clickdblclick事件。触发事件的顺序因浏览器而异,有些click在之前收到两个事件dblclick,有些则只收到一个。双击灵敏度(被检测为双击的最大点击间隔时间)可能因操作系统和浏览器而异,并且通常是用户可配置的。

What you're seeing is exactly what is expected (depending on the browser of course). The only way around your problem is to set a timer and manually differentiate between a single- and double-click yourself; then you'll have to adjust the timer value and check various browsers and operating systems until you get something that sort of pretends to work in most place.

您所看到的正是预期的(当然取决于浏览器)。解决问题的唯一方法是设置计时器并手动区分单击和双击;那么你就必须调整定时器值并检查各种浏览器和操作系统,直到你得到一些假装在大多数地方都能工作的东西。

I'd strongly recommend that you use separate controls with single-click actions instead. Double-click is pretty unfriendly period and we only put up with it because we're used to it.

我强烈建议您使用带有单击操作的单独控件。双击是非常不友好的时期,我们只能忍受它,因为我们已经习惯了。

回答by Andrei Rosca

Try adding return false;at the end of select & toggle.

尝试return false;在选择和切换的末尾添加。