移动端html标签点击事件onclick失效问题解决

今天测试突然提了这个bug,移动端onclick事件点击失效问题,网上找了好多方法,都是解决在js中绑定点击事件的问题

在js中绑定点击事件用click是兼容的

<div id="test">click</div>`
<script>
	var fun = () => {
				console.log(Math.random());
			}
	document.getElementById('test').addEventListener('click', fun);
</script>

这种写法是兼容的,亲测过web浏览器和移动浏览器都是可以兼容的,click改成tap,touchstart,touchend,touchmove的话,只有移动浏览器可以生效,web不生效。

但是我遇到的是在html标签上的点击事件问题

<a href="#" onclick="console.log(Math.random());">onclick</a>

经过多次的尝试最后发现标签上可以使用ontouchstart,ontouchend,ontouchmove这三个事件

<a href="#" ontouchstart="console.log(Math.random());">onclick</a>

这三个事件都可以在移动端上使用,不过不兼容web端

猜你喜欢

转载自blog.csdn.net/weixin_39675478/article/details/87292630