【问题标题】:Click event jquery on safari mobile在 safari mobile 上单击事件 jquery
【发布时间】:2019-05-08 08:17:15
【问题描述】:

jquery 点击事件在移动 safari 中不起作用。

我尝试了“curson:指针”,也降级/升级 jquery 版本,但没有任何效果。

https://codepen.io/larsen1982/pen/yWYNLj

$(".next").click(function(){
    if(animating) return false;
    animating = true;

这是 jquery: http://thecodeplayer.com/uploads/js/jquery-1.9.1.min.js

在桌面、ipad 和 Android smarthpone 浏览器中,代码运行良好。 在 chrome 和 safari mobile (iphone) 中,这些按钮不起作用。

【问题讨论】:

标签: jquery touch mobile-safari jquery-click-event


【解决方案1】:

尝试添加touchstart:

$(".next").on('touchstart click', function(){
    ...
});

【讨论】:

  • 我觉得您的代码中还有其他问题。这是一个非常简单的任务。
  • 还是不行
【解决方案2】:

问题是 iPhone 不会引发点击事件。他们引发“触摸”事件。添加以下代码即可。

function touchHandler(event){
    var touches = event.changedTouches,
        first = touches[0],
        type = "";

    switch(event.type)
    {
       case "touchstart": type = "mousedown"; break;
       case "touchmove":  type = "mousemove"; break;        
       case "touchend":   type = "mouseup"; break;
       default: return;
    }

    var simulatedEvent = document.createEvent("MouseEvent");
    simulatedEvent.initMouseEvent(type, true, true, window, 1, 
                          first.screenX, first.screenY, 
                          first.clientX, first.clientY, false, 
                          false, false, false, 0/*left*/, null);

    first.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}

function init() {
    document.addEventListener("touchstart", touchHandler, true);
    document.addEventListener("touchmove", touchHandler, true);
    document.addEventListener("touchend", touchHandler, true);
    document.addEventListener("touchcancel", touchHandler, true);    
}

【讨论】:

  • 您的解决方案适用于 chrome 手机 (iphone),但不适用于 safari 手机。
  • 你知道 safari mobile 的一些技巧吗?可能是 标签的问题吗?
猜你喜欢
  • 2020-09-08
  • 1970-01-01
  • 2013-12-20
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
相关资源
最近更新 更多