如何解决两次触发点击事件的麻烦? [英] How can I trouble shoot click events being triggered twice?

查看:74
本文介绍了如何解决两次触发点击事件的麻烦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我有一个单页应用。

  • 它使用backbone.js。

  • 通过鼠标触发器单击一次事件。

  • 通过触摸设备触发器点击两次事件。

  • 取消绑定单击事件会在触摸设备上停止。

  • I have a one page app.
  • It uses backbone.js.
  • Click events via mouse trigger once.
  • Click events via touch device trigger twice.
  • Unbinding the one click event stops both on touch devices.

我无法计算从哪里开始寻找。

I can't figure out where to start looking.

这是JS:

$('.classy').on('click', 'button', function(){
    console.log('clicked');
})

我需要一些帮助来弄清楚如何解决这个问题。我知道我没有提供足够的信息来获得真正的答案。对我来说令人困惑的部分是,这只发生在触摸设备上。如果我不小心绑定了两个事件或创建了同一个视图的两个实例,那么鼠标点击也不会发生这种情况吗?

I need some help figuring out how to trouble shoot this. I know I haven't given enough information to receive a real answer. The confusing part to me is that this only happens on touch devices. If I was accidentally binding two events or creating two instance of the same view it wouldn't it happen for mouse clicks too?

谢谢。

编辑:我尝试通过jQuery Mobile使用tap事件。这有一个奇怪的反应。它会触发一次事件,看起来就像是完成了,但是当你下次触摸屏幕上的任何地方时,它会再次触发事件。 ......奇怪,有什么想法吗?

我终于找到了问题。它来自分层的iScrolls。我不得不在这一点上破解lib,可能是解决这个问题的更好方法,但说明了这一点。

I finally found the problem. It was coming from layered iScrolls. I had to hack the lib at this point, probably a much better way to fix this but illustrates the point.

if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA' && window.iScrollClickFIX != true) {                        
    window.iScrollClickFIX = true;
    setTimeout(function(){
        window.iScrollClickFIX = false;
    }, 1)

感谢大家的帮助。

推荐答案

这可能不是真正的解决方案......只是给予想法

This may not be the actual solution...Just giving a thought

$('.classy').die('click').on('click', 'button', function(){ 
    console.log('clicked'); 
}) 

这篇关于如何解决两次触发点击事件的麻烦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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