我正在创建一个应该可以在移动设备和 PC 上运行的 jQuery 移动应用程序。我正在使用 jQuery Finger 插件 ( http://ngryman.sh/jquery.finger/ ) 来检测手指事件:
$('body').on('tap', 'image', function(e) {
console.log(e);
});
问题是点击事件被触发了两次。
[1] v.Event {type: "tap", x: 600, y: 374, orientation: null, end: false…}
[2] v.Event {originalEvent: v.Event, type: "tap", timeStamp: 1440756290573, jQuery183031088717421516776: true, toElement: image#7…}
当我在开发者工具中折叠第二个事件信息时,类型显示“vclick”。
...
shiftKey: false
srcElement: image#7
target: image#7
timeStamp: 1440756501237
toElement: image#7
type: "vclick"
view: Window
which: 1
__proto__: Object
...
如何防止这种情况发生,以便事件仅运行 jQuery 手指点击事件而不运行 vclick?
最佳答案
您可以通过检查事件的特定字段来区分事件(忽略它们或进行特殊处理)。
在这种特殊情况下,我看到了 originalEvent
字段中的差异。因此,为了仅处理第二个事件,您可以使用类似这样的方法(event.originalEvent
被替换为函数的参数 originalEvent
用于测试目的):
$(document).on('tap', function(event, originalEvent) {
if (originalEvent) {
console.log('Time to handle event');
//code code code
}
else {
console.log('Ignoring event');
}
});
$(document).trigger('tap');
$(document).trigger('tap', ['v.Event']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
关于javascript - jQuery Mobile 点击事件触发 vclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32268582/