构建 Web 应用程序时无法触发点击事件。从普通的 Safari 实例运行时,它运行完美,但从主屏幕运行时,它会失败。
$("a.applink").live('click', clickHandler);
var clickHandler = function(e) {
console.log(e);
e.preventDefault();
}
从主屏幕运行时,“e”为空。如果我用“touchend”替换“click”,它确实有效,但 touchend 没有给出正确的行为(即,如果您碰巧触摸一个链接开始滚动,它会触发意外点击)。
是否有合适的解决方案?
最佳答案
这是我的做法:
本质上,当您浏览页面时,您需要点击或滚动。 (好吧,还有其他的东西,比如捏和滑动,你可以稍后再弄清楚)......
所以在点击你的“touchstart”之后将是一个“touchend” 在滚动条上,您的“touchstart”后跟一个“touchmove”
使用 Jq 1.7...在其他版本上你可以使用 .bind()
function nextEvent() {
//behaviour for end
$(this).on('touchend', function(e){
/* DO STUFF */
$(this).off('touchend');
});
//behaviour for move
$(this).on('touchmove', function(e){
$(this).off('touchend');
});
}
$(element).on('touchstart', this, nextEvent);
基本上,当“touchstart”发生时,我将 Action 绑定(bind)到“touchend”和“touchmove”。
'Touchend' 做任何我想让点击做的事,然后自行解除绑定(bind) 'Touchmove' 除了取消绑定(bind) 'touchend' 基本上什么都不做
这样,如果你点击你就会得到 Action ,如果你滚动除了滚动什么都不会发生..
关于jQuery 单击事件未触发 - ios 主屏幕 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12218883/