jQuery click 和 tap 事件有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12422944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:39:29  来源:igfitidea点击:

What is the difference between the click and tap events?

jqueryjquery-mobiletouch-event

提问by Tiger

I am developing an app that runs on Android/iOS and desktop computers.

我正在开发一个在 Android/iOS 和台式计算机上运行的应用程序。

Should I use the clickor the tapevent?

我应该使用click还是tap事件?

What are the differences between them?

它们之间有什么区别?

Will the 'tap' work on desktop? if not, (and I have to use click) am I missing any advantages that the taphas over the click?

'tap' 可以在桌面上使用吗?如果没有,(我必须使用click)我是否错过了tap它比 的任何优势click

采纳答案by ghoti

Touch events and mouse events are entirely different beasts. They are not interchangeable.

触摸事件和鼠标事件是完全不同的野兽。它们不可互换。

That said, I'm using jQuery Touch Punchwhich maps touch events to their mouse event analogs, so a tap becomes the same thing as a click. This makes it possibly to use all the standard jQuery UI goodness without having to do any special touch event programming. It's extremely simple to use, and so far has worked perfectly for me on both iDevices and Android.

也就是说,我正在使用jQuery Touch Punch,它将触摸事件映射到它们的鼠标事件类似物,因此点击与点击是一样的。这使得可以使用所有标准的 jQuery UI 优点,而无需进行任何特殊的触摸事件编程。它使用起来非常简单,到目前为止在 iDevices 和 Android 上对我来说都非常完美。

回答by tuned

You can use .on()to bind a function to multiple events:

您可以使用.on()将函数绑定到多个事件:

$('#element').on('click tap', function() {
    ...
});

Thanks to @bergie3000 for pointing to this

感谢@bergie3000 指出这一点

回答by Rokoco

Sorry for bringing up old post, but for anyone who's working on jQuery with Phonegap:

很抱歉提出旧帖子,但对于使用 Phonegap 使用 jQuery 的任何人:

"click" event will not be triggered if assigned on "div", "p" etc. elements that were not suppose to be clicked, when "tap" makes it working perfectly on any element! That took me ages to notice.

如果在“div”、“p”等不应该被点击的元素上分配“点击”事件,则不会触发“点击”事件,当“点击”使其在任何元素上完美运行时!我花了很长时间才注意到。

Conclusion, if you do develop something with jQuery mobile, I would suggest using "tap" instead of "click", as it might cause some issues

结论,如果你确实用 jQuery mobile 开发了一些东西,我建议使用“tap”而不是“click”,因为它可能会导致一些问题

回答by Phill Pafford

I would look into the jQM Documentation for Events, the current docs are here:

我会查看事件的 jQM 文档,当前文档在这里:

for a Mobile device use touch events, if you need mouse events jQM provides Virtual mouse events for this as well.

对于移动设备,请使用触摸事件,如果您需要鼠标事件,jQM 也为此提供了虚拟鼠标事件。

Virtual mouse events
We provide a set of "virtual" mouse events that attempt to abstract away mouse and touch events. This allows the developer to register listeners for the basic mouse events, such as mousedown, mousemove, mouseup, and click, and the plugin will take care of registering the correct listeners behind the scenes to invoke the listener at the fastest possible time for that device. In touch environments, the plugin retains the order of event firing that is seen in traditional mouse environments, so for example, vmouseup is always dispatched before vmousedown, and vmousedown before vclick, etc. The virtual mouse events also normalize how coordinate information is extracted from the event, so in touch based environments, coordinates are available from the pageX, pageY, screenX, screenY, clientX, and clientY properties, directly on the event object.

虚拟鼠标事件
我们提供了一组试图抽象掉鼠标和触摸事件的“虚拟”鼠标事件。这允许开发人员为基本鼠标事件注册侦听器,例如 mousedown、mousemove、mouseup 和 click,插件将负责在幕后注册正确的侦听器,以便在该设备的最快时间调用侦听器. 在触摸环境中,该插件保留了在传统鼠标环境中看到的事件触发顺序,因此,例如,vmouseup 总是在 vmousedown 之前调度,vmousedown 在 vclick 之前调度等。虚拟鼠标事件也规范了如何从中提取坐标信息事件,因此在基于触摸的环境中,坐标可从 pageX、pageY、screenX、screenY、clientX 和 clientY 属性获得,

I'm not saying the 'click' event wont work as it still does, for example you can click a button. But there will be different events for Mobile devices verses Desktops

我并不是说 'click' 事件不会像现在那样工作,例如您可以单击一个按钮。但是移动设备和台式机会有不同的事件