如何确定客户端是否为触摸设备 [英] How to determine if the client is a touch device

查看:165
本文介绍了如何确定客户端是否为触摸设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何漂亮而干净的方法或技巧来确定用户是否在触摸设备上?

is there any nice and clean method or trick to find out if the user is on a touch-device or not?

我知道有些东西比如
var isiPad = navigator.userAgent.match(/ iPad / i)!= null;

但我只是想知道是否有一个技巧来确定用户是否在Touch设备上?
因为那里有更多的触控设备和平板电脑,只有iPad。

but I simply wonder if there is a trick to generally determine if the user is on Touch device? Because there are a lot more touch devices and tablets out there then just iPads.

谢谢。

推荐答案

您可以使用以下JS函数:

You can use the following JS function:

function isTouchDevice() {
   var el = document.createElement('div');
   el.setAttribute('ongesturestart', 'return;'); // or try "ontouchstart"
   return typeof el.ongesturestart === "function";
}

资料来源:检测基于触摸的浏览

请注意以上代码测试浏览器是否支持触摸,而不是设备。

Please note the above code only tests if the browser has support for touch, not the device.

相关链接:

  • Optimize website for touch devices
  • What is the best way to detect a mobile device in jQuery?
  • What's the best way to detect a 'touch screen' device using JavaScript?
  • Mozilla.org Detecting touch: it’s the ‘why’, not the ‘how’

中可能存在检测href =http://jquerymobile.com/ =nofollow noreferrer> jquery for mobile jtouch

这篇关于如何确定客户端是否为触摸设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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