【问题标题】:Click event not working on iPad with touch event点击事件在带有触摸事件的 iPad 上不起作用
【发布时间】:2012-06-11 06:47:49
【问题描述】:

我在页面链接上使用点击事件。它在 iPad 的Safari 上运行良好,但是当我在同一页面上使用触摸事件时,点击事件停止工作;只有触摸事件在 iPad 上有效。

点击事件:

 link.onclick = onLinkClick;

触摸事件:

$('#sdiv').bind({
    'touchstart': function (e) {
        onTouchStart(e, sdiv);
    }
});

$('#sdiv').bind({
    'touchend': function (e) {
        onTouchEnd(e);
    }
});

$('#sdiv').bind({
    'touchmove': function (e) {
        onTouchMove(e);
    }
});

$('#sdiv').bind({
    'touchcancel': function (e) {
        onTouchCancel(e);
    }
});

【问题讨论】:

  • link 元素是否在 #sdiv 元素内?如果是这样,请确保您没有在触摸处理程序中使用event.preventDefault()
  • 否,如果我从触摸处理程序中删除 event.preventDefault() 然后单击事件有效,但它会停止触摸事件。
  • 是的,如果你按照文档使用event.preventDefault(),你不能同时拥有两者。要么将 link 元素从 #sdiv 中移出,要么使用 touchstarttouchend 事件和计时器手动实现对 link 元素的点击。
  • 好的,现在我得到了我将用于 ipad 的答案var platform = navigator.platform; if( platform === 'iPad') { _link.ontouchend = onLinkClick;} else { _link.onclick = onLinkClick; }
  • 你可能想探索一个像hammer.js这样的库来为你管理触摸事件eightmedia.github.com/hammer.js

标签: jquery ipad html touch


【解决方案1】:

最后,我区分了两个平台,iPaddesktop。使用“navigator.platform”,如果是 iPad,则 touch 事件 有效,否则 Click 事件

 var platform = navigator.platform; 

 if( platform == 'iPad') { _link.ontouchend = onLinkClick;} 

  else { _link.onclick = onLinkClick; }

谢谢大家,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    相关资源
    最近更新 更多