onClick事件会在触摸屏设备上触摸吗?

时间:2016-05-17 10:10:00

标签: javascript html css

我在我的网站上使用了onclick个事件。但是当我在google chromes的开发者模式的移动视图中打开它时,触摸用鼠标点击的元素没有任何反应。所以我的问题是:

我是否还必须在所有触摸屏设备上添加ontouch个事件以及onclick个事件或onClick事件工作?

P.S:您可以在此处查看我的所有代码:https://github.com/SycoScientistRecords/sycoscientistrecords.github.io/

或在直播网站:http://sycoscientistrecords.github.io

并且我没有在真实手机上测试过该网站。

3 个答案:

答案 0 :(得分:8)

onmousedown在触摸屏上工作正常;我已经好几次使用它,从来没有遇到任何问题。

您可以考虑使用onclick代替type Company struct { Id bson.ObjectId `bson:"_id,omitempty"` CompanyName string Slug string CompanyUsers []CompanyUser } type CompanyUser struct { UserName string Email string FullName string } 。或使用jQuery检测点按。

答案 1 :(得分:0)

onclick可能无法在触摸设备上运行,我遇到了这个问题,ontouchstart事件对它进行了排序。 如果您使用ontouchstart和onclick观看,则不会两次触发该事件。

这是另一篇相关文章 onClick not working on mobile (touch)

答案 2 :(得分:0)

我发现 this detailed writeup at MDN 很有帮助。特别是:

<块引用>

浏览器可能触发触摸事件和鼠标事件以响应相同的用户输入[强调我的]

<块引用>

元素的触摸事件处理程序应该调用 preventDefault() 并且不会调度额外的鼠标事件

因此,您的 touchstarttouchend 侦听器可以调用 evt.preventDefault(),而您的 mousedown / mouseup 侦听器不会触发,因为它们在链中稍后出现.

在 Angular 中,通过将 (click)="doSomething()" 更改为 (mouseup)="doSomething(false)" (touchend)="doSomething(true); $event.preventDefault()",我能够检测我是使用鼠标还是笔记本电脑的触摸屏点击了按钮。该方法通过 true 用于触摸事件和 false 用于鼠标事件。