繁体   English   中英

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

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

在下面的脚本中,您将在下面找到“ click”和“ touchstart”事件。 最初,这是“单击”事件,直到我们发现iPhone和iPad不能工作,因为它需要“ touchstart”才能工作。

因此,我将它们都包括在内,因此可以在iPhone / iPad上使用。

然后我遇到了Android问题,其中“ click”和“ touchstart”都被触发,导致执行两次。

那么,针对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();
        });
    });

我们在项目中解决类似问题的方式是这样的

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

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

在您的活动分配中

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

关于isMobile函数的说明。

桌面浏览器不支持orientation因此我们可以检测当前浏览器是桌面浏览器还是移动浏览器。

如果undefined window.orientation ,则它是桌面浏览器,将不支持触摸事件。

如果不是,则仅使用触摸事件。 您在EVENT_CONFIG确定

暂无
暂无

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

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