今天测试突然提了这个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端