【发布时间】:2012-04-18 22:06:06
【问题描述】:
当访问者点击图片时,click 事件将被触发。但是,当有人触摸图像时,将触发相同的 click 事件,即使 touchstart 事件也可用。
我喜欢实际点击(鼠标)事件和触摸事件的不同行为。奇怪的是,在智能手机上使用时甚至会触发mouseup 事件。无论如何,您可以将鼠标与触摸事件分开吗?
【问题讨论】:
标签: javascript events touch mouse
当访问者点击图片时,click 事件将被触发。但是,当有人触摸图像时,将触发相同的 click 事件,即使 touchstart 事件也可用。
我喜欢实际点击(鼠标)事件和触摸事件的不同行为。奇怪的是,在智能手机上使用时甚至会触发mouseup 事件。无论如何,您可以将鼠标与触摸事件分开吗?
【问题讨论】:
标签: javascript events touch mouse
event.preventDefault();
成功了,希望对大家有所帮助!
【讨论】:
event.preventDefault() 添加到touchstart 回调中,这将不起作用。至少不是 iOS 8.4 Safari 中的链接。您必须将其添加到 click 事件处理程序。
你可以标准化一个事件..
查看我对这个问题的回答:
Click event called twice on touchend in iPad
您也可以在 jQuery mobile 源代码中寻找灵感:http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.js 从第 982 行开始
/*
* "events" plugin - Handles events
*/
(function( $, window, undefined ) {
// add new event shortcuts
$.each( ( "touchstart touchmove touchend orientationchange throttledresize " +
"tap taphold swipe swipeleft swiperight scrollstart scrollstop" ).split( " " ), function( i, name ) {
$.fn[ name ] = function( fn ) {
return fn ? this.bind( name, fn ) : this.trigger( name );
};
$.attrFn[ name ] = true;
});
....
查看点击事件:(第 1049 行)
$.event.special.tap = {
【讨论】:
在 touchstart 后防止点击是一种复杂的方法。 touchstart 后传播的点击称为幻点击。
Google 已经实施了一个解决方案。给你。。
http://code.google.com/mobile/articles/fast_buttons.html#ghost
【讨论】: