如何绑定两者MOUSEDOWN和Touchstart,但不是这两个反应? Android的,JQuery的 [英] How to bind both Mousedown and Touchstart, but not respond to both? Android, JQuery

查看:656
本文介绍了如何绑定两者MOUSEDOWN和Touchstart,但不是这两个反应? Android的,JQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作的一个网站,也可于移动和需要绑定两个touchstart和鼠标按下操作。

Working on a website that is also viewable on mobile and need to bind an action on both touchstart and mousedown.

看起来像这样

 $("#roll").bind("mousedown touchstart", function(event){

 someAction();

它工作正常在iPhone上,但在Android上它回应了两次。

It works fine on Iphone, but on Android it responds twice.

event.stopPropagation();
event.preventDefault();

添加此code固定它为Android浏览器,而不是Android的默认浏览器。任何其他的技巧,可以解决所有的android的问题?

Adding this code fixed it for Android Chrome, but NOT for Android default browser. Any other tricks that can fix the problem for all android?

推荐答案

我一直在使用这个功能:

I have been using this function:

//touch click helper
(function ($) {
    $.fn.tclick = function (onclick) {
        this.bind("touchstart", function (e) { onclick.call(this, e); e.stopPropagation(); e.preventDefault(); });
        this.bind("click", function (e) { onclick.call(this, e); });   //substitute mousedown event for exact same result as touchstart         
        return this;
    };
})(jQuery);

更新:修改答案,支持鼠标和触摸事件一起。

UPDATE: Modified answer to support mouse and touch events together.

这篇关于如何绑定两者MOUSEDOWN和Touchstart,但不是这两个反应? Android的,JQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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