jQuery mobile-对于每个实时点击事件是否应该有一个等效的点击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6235794/
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 20:34:57  来源:igfitidea点击:

jQuery mobile- For every live tap event should there be an equivalent click event?

jqueryjquery-selectorsjquery-mobile

提问by TaylorMac

I have replaced the jQuery live click events to jQuery mobile tap events to increase responsiveness.

我已将 jQuery 实时点击事件替换为 jQuery 移动点击事件以提高响应能力。

I have a feeling this was a bad idea for compatibility reasons.

我有一种感觉,出于兼容性原因,这是一个坏主意。

Is it necessary to have both events, and is there any way to write them both for the same function?

是否有必要同时拥有这两个事件,有没有办法将它们都编写为同一个函数?

Such as ('click','tap')

如 ('click','tap')

回答by J.T.Sage

Billy's answer is incredibly complete and actually worked quite well the few times I used it. Additionally however, you may want to look at the vmouse plugin in JQuery Mobile, it is an attempt to abstract mouse events:

比利的回答非常完整,实际上在我使用它的几次中效果很好。此外,您可能还想查看JQuery Mobile中的 vmouse 插件,它是一种抽象鼠标事件的尝试:

 // This plugin is an experiment for abstracting away the touch and mouse
 // events so that developers don't have to worry about which method of input
 // the device their document is loaded on supports.

-- https://github.com/jquery/jquery-mobile/blob/master/js/vmouse.js

-- https://github.com/jquery/jquery-mobile/blob/master/js/vmouse.js

I've been playing with it on a project I'm working on, it seems pretty responsive these days. To use, something like:

我一直在我正在做的一个项目中使用它,这些天它似乎非常敏感。要使用,例如:

$('selector').bind('vclick', function () { ...

or

或者

$('selector').bind('vmousedown', function () { ...

回答by Billy Moon

You can bind multiple events in one call like this:

您可以在一次调用中绑定多个事件,如下所示:

$('selector').bind('click tap',function(){ ... })

This might be fine in some browsers/mobiles, however, this might make the events fire twice on some devices who trigger both the tap and click.

这在某些浏览器/移动设备中可能没问题,但是,这可能会使事件在某些同时触发点击和点击的设备上触发两次。

You can fix this by doing some kind of device/feature detection and adding the appropriate handler only like this...

您可以通过进行某种设备/功能检测并仅像这样添加适当的处理程序来解决此问题...

$('selector').bind( myCustomDetectionFunction() ? 'click' : 'tap' ,function(){ ... })

Additionally, I think touchstartand mousedownare better events to choose. This is because, after a touch, the click event does not fire until a delay has passed, as the system is allowing the chance for a second touch to make it a double click or for it to become a swipe gesture and so on. The touchstartevent fires immediately, as does mousedownso should be more responsive.

此外,我认为touchstart并且mousedown是更好的事件选择。这是因为,在触摸之后,直到经过延迟才会触发点击事件,因为系统允许第二次触摸有机会使其成为双击或使其成为滑动手势等等。该touchstart事件立即触发,如不mousedown这样应该更敏感。

回答by Marcel Colomb

We've developed a small script to solve that problem. Just include it on a global level and your click events will be fired immediately without any problems with the delayed event.

我们开发了一个小脚本来解决这个问题。只需在全局级别包含它,您的点击事件就会立即触发,而不会出现延迟事件的任何问题。

https://github.com/cargomedia/jquery.touchToClick

https://github.com/cargomedia/jquery.touchToClick

回答by h3.

It seems jQuery mobile has already an event which does just that:

似乎 jQuery mobile 已经有一个事件可以做到这一点:

$(function(){
    $('selector').bind('vclick', function(e){
        alert('test');    
        return false;
    });
});

回答by user2449231

You can use the vmouse plugin from jQuery. This will resolve the 300ms delay on click events (mobile only) as well as cases where both click and touch events are triggered.

您可以使用 jQuery 中的 vmouse 插件。这将解决点击事件(仅限移动设备)的 300 毫秒延迟以及同时触发点击和触摸事件的情况。

To get just the vmouse plugin, use the jQuery Mobile Download Builder. Include it after jQuery but before any scripts that will depend on this plugin.

要获得 vmouse 插件,请使用jQuery Mobile Download Builder。将它包含在 jQuery 之后,但在任何依赖此插件的脚本之前。

The relevant event is vclick, basic use is as follows:

相关事件是vclick,基本使用如下:

$(".selector").on( "vclick", function( event ) {
    // To execute
});