移动端a标签跳转方案

  • a标签在促发touchend事件时,a标签就会跳转;
  • touchmove最终也会触发touchend,所以会导致误触;

代码显示

// 全面禁止移动端事件的默认行为
document.addEventListener('touchstart',function(ev){
	ev = ev || event;
	ev.preventDefault();
})
// 获取页面中所有的a标签
var aNodes = document.querySelectorAll('a');
for(var i=0;i<aNodes.length;i++){
	aNodes[i].addEventListenner('touchstart',function(){
		this.Ismoved = false;
	})
	aNodes[i].addEventListenner('touchmove',function(){
		this.Ismoved = true;
	})
	aNodes[i].addEventListenner('touchend',function(){
		if(!Ismoved){
			location.href = this.href;
		}
	})
}
发布了133 篇原创文章 · 获赞 0 · 访问量 1685

猜你喜欢

转载自blog.csdn.net/weixin_43269800/article/details/104861568