OGeek|极客世界-中国程序员成长平台

标题: jquery - 无意中停止触摸端触发链接 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:54
标题: jquery - 无意中停止触摸端触发链接

我正在使用 touchend 事件来防止 ios 需要两次触摸才能触发 href 链接。 这工作正常,但它在滚动时无意中触发了链接。

我知道解决方案是实现 touchstart 以查看是否有移动,但我是 jquery 新手,我不确定如何应用它。

这是touchend代码

$('a').on('touchend', function(e) {
var el = $(this);
var link = el.attr('href');
window.location = link;
});

希望有人能帮忙。

谢谢



Best Answer-推荐答案


好的,这就是我使用 this post 中的代码解决此问题的方法

var dragging = false;
$("a").on("touchmove", function(){
  dragging = true;
});

$("a").on("touchend", function(e){
  if (dragging){
e.preventDefault();
}
else {var el = $(this);
var link = el.attr('href');
window.location = link;
}    

});

$("a").on("touchstart", function(){
dragging = false;
});

这对我有用。

关于jquery - 无意中停止触摸端触发链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22451908/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4