如何确定客户端是否为触摸设备

mat*_*att 30 javascript jquery touch tablet

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

我知道有类似的东西 var isiPad = navigator.userAgent.match(/iPad/i) != null;

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

谢谢.

mpl*_*jan 28

您可以使用以下JS函数:

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

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

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

相关链接:

可能在jquery中检测到移动jtouch


Pra*_*inz 14

if ("ontouchstart" in document.documentElement)
{
  alert("It's a touch screen device.");
}
else
{
 alert("Others devices");
}
Run Code Online (Sandbox Code Playgroud)

我在这里和那里浏览了很多之后找到的最简单的代码


Gco*_*oop 13

包括现代化器,这是一个微小的特征检测库.然后你可以使用下面的东西.

if (Modernizr.touch){
   // bind to touchstart, touchmove, etc and watch `event.streamId`
} else {
   // bind to normal click, mousemove, etc
}
Run Code Online (Sandbox Code Playgroud)

  • 特征检测> UA检查. (2认同)