查找设备是否为触摸屏然后应用触摸事件而不是单击事件 [英] Find if device is touch screen then apply touch event instead of click event

查看:67
本文介绍了查找设备是否为触摸屏然后应用触摸事件而不是单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个phonegap应用程序,但是当我开始知道触发点击事件而不是touchevent需要300MS。

I am creating a phonegap application, but as I came to know that it takes 300MS to trigger click event instead of touchevent.

我不想申请两个事件。有没有办法知道它是没有现代化器的触摸设备。

I don't want to apply both event. Is there any way to know if it's touch device without modernizer.

这是假设的jquery代码

Here is jquery code for assumption

$('#id').on('click',funciton(e){
  alert('id was clicked');
});

无论如何都要使用纯JS / jQuery,因为phonegap应用已经占用了我想要的更多内存尽可能少用库。

is there anyway to do it with pure JS/jQuery as phonegap application already takes more memory I want to use less library as I can.

推荐答案

我的意思是你应该 Modernizr 但是......

I mean really you should Modernizr but...

var supportsTouch = 'ontouchstart' in window || navigator.msMaxTouchPoints;
var eventType = supportsTouch ? 'ontouchstart' : 'click';

然后声明你的事件监听器:

Then declare your event listeners as such:

$('#id').on(eventType, function(e) {
  alert('id was clicked');
});

这篇关于查找设备是否为触摸屏然后应用触摸事件而不是单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆