简体   繁体   English

如何在jquery mobile中监听两个事件并仅触发一个功能?

[英]how to listen for two events and only fire one function in jquery mobile?

I have this: 我有这个:

$('li.clickable').live('click tap', function() {
   console.log("hello");
   )};

If I listen like this and click on the list item, two events are firing = I'm getting two "hellos". 如果我这样听并单击列表项,则会触发两个事件=我得到两个“ Hellos”。 I need to fire a function with every "click tap", but only once! 我需要在每次“点击”时触发一个函数,但是只需触发一次!

Is there any way to do this. 有什么办法可以做到这一点。 I cannot use "one" because then the function will go silent after the first click, isn't it? 我不能使用“ one”,因为在第一次单击后该功能将静音,不是吗?

Thanks for pointer! 感谢您的指导!

EDIT : 编辑
if I omit either click or tap, the function still fires twice. 如果我没有单击或单击,该功能仍会触发两次。 Any way to prevent this? 有什么办法可以防止这种情况?

jQM Docs: 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. 这允许开发人员为基本的鼠标事件注册侦听器,例如mousedown,mousemove,mouseup和click,并且插件将负责在后台注册正确的侦听器,以在尽可能快的时间为该设备调用侦听器。 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. 在触摸环境中,该插件保留了在传统鼠标环境中看到的事件触发顺序,例如,总是在vmousedown之前调度vmouseup,在vclick之前调度vmousedown等。虚拟鼠标事件还规范了如何从中提取坐标信息。事件,因此在基于触摸的环境中,可以直接在事件对象上从pageX,pageY,screenX,screenY,clientX和clientY属性获得坐标。

vclick vclick

Normalized event for handling touchend or mouse click events. 用于处理触摸端或鼠标单击事件的规范化事件。 On touch devices, this event is dispatched AFTER vmouseup. 在触摸设备上,此事件在vmouseup 之后调度。

Try using: 尝试使用:

$('li.clickable').live('vclick', function() {
   console.log("hello");
)};

Also you can test which event is firing 您也可以测试触发哪个事件

$('li.clickable').live('click tap', function(e) {
    console.log("Which Event: "+e.which + " Event Type: "+e.type);
)};

Since your action also fires twice if only listening to click you might be running into some user or system weirdness. 由于如果仅听click您的操作也会触发两次,因此您可能会遇到一些用户或系统的怪异现象。

We had all sorts of problems when capacitive touchscreen started to become widespread and people (eg in gloves) would start double-triggering. 当电容式触摸屏开始普及并且人们(例如戴着手套)将开始两次触发时,我们会遇到各种各样的问题。 What helps against a user-doubletap might help against a system-doubletap: 有助于防止用户双击的因素可能有助于防止系统被双击:

//global scope:
var triggertimeouts={}

and

$('li.clickable').live('click tap', function() {
   if (!triggertimeouts.myclicktap) {
      triggertimeouts.myclicktap=window.setTimeout('triggertimeouts.myclicktap=false;',250);
      console.log("hello");
   }
   )};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM