Stop touchend firing links unintentionally

妖精的绣舞 提交于 2020-01-02 18:23:33

问题


I'm using the touchendevent to prevent ios from requiring two touches to fire href links. This works fine however it is firing the links unintentionally when scrolling.

I know that the the solution is to implement the touchstart to see if there is movement, but I'm a jquery novice and I'm not sure how apply this.

Here is the touchendcode

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

Hope someone can help.

Thanks


回答1:


Ok this is what is working for me to solve this using code from 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;
});

This works for me.




回答2:


Use preventDefault:

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



回答3:


For me this one works

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


来源:https://stackoverflow.com/questions/22451908/stop-touchend-firing-links-unintentionally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!