简体   繁体   English

on(“ click”,foo)和on(“ touchstart”,foo)在Android与iPhone中引起奇怪的问题

[英]on(“click”, foo) and on(“touchstart”, foo) causing strange issues in Android vs iPhone

In the script below, you'll find both "click" and "touchstart" event below. 在下面的脚本中,您将在下面找到“ click”和“ touchstart”事件。 Orignally it was "click" event until we discovered that iPhone and iPad don't work because it required "touchstart" to work. 最初,这是“单击”事件,直到我们发现iPhone和iPad不能工作,因为它需要“ touchstart”才能工作。

So, I included both of them so it would work for iPhone/iPad. 因此,我将它们都包括在内,因此可以在iPhone / iPad上使用。

Then I run into Android issue where both "click" and "touchstart" get fired, leading to 2 times execution. 然后我遇到了Android问题,其中“ click”和“ touchstart”都被触发,导致执行两次。

So, what's the recommened workaround to this issues for both iPhone and Android? 那么,针对iPhone和Android的此问题的推荐解决方法是什么?

    //Saved Vehicle - Button...
    $(document).on('click touchstart', 'div[id^=RecordViewSheet]', function () {
        var dataVin = $(this).attr("data-vin");
        var dataStockNumber = $(this).attr("data-stock-number");

        ftnThrobblerAnimationBegin3().done(function () {
            httpFormSubmissionPostMethod("InspectionSheet.cshtml", "formStockNumber=" + dataStockNumber + "&formVin=" + dataVin);

            ftnThrobblerAnimationEnd3();
        });
    });

The way we tackled similar problem in our project was like this 我们在项目中解决类似问题的方式是这样的

function isMobile(){
    if(typeof window.orientation !== 'undefined') return true;
    return false;
}

var EVENT_CONFIG = {
    CLICK: isMobile()?'touchstart':'click'
}

and in your event assignment 在您的活动分配中

$(document).on(EVENT_CONFIG.CLICK, 'div[id^=RecordViewSheet]', function () {

Explanation on the isMobile function. 关于isMobile函数的说明。

Desktop browsers do not support orientation so that is how we detect if the current browser is that of desktop or of mobile. 桌面浏览器不支持orientation因此我们可以检测当前浏览器是桌面浏览器还是移动浏览器。

If window.orientation is undefined then it is a desktop browser and won't support touch events. 如果undefined window.orientation ,则它是桌面浏览器,将不支持触摸事件。

If not, then use only touch events. 如果不是,则仅使用触摸事件。 You determine that in EVENT_CONFIG 您在EVENT_CONFIG确定

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

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