有没有什么好的、干净的方法或技巧来发现用户是否在使用触摸屏设备?
我知道有像var isiPad = navigator.userAgent.match(/iPad/i) != null;
这样的东西
但我只是想知道是否有一个技巧来确定用户是否在使用Touch设备?因为市场上的触控设备和平板电脑比iPads多得多。
谢谢。
发布于 2011-06-07 16:41:22
您可以使用以下JS函数:
function isTouchDevice() {
var el = document.createElement('div');
el.setAttribute('ongesturestart', 'return;'); // or try "ontouchstart"
return typeof el.ongesturestart === "function";
}
来源:Detecting touch-based browsing。
请注意,上面的代码只测试浏览器是否支持触摸,而不是设备。
相关链接:
在jquery for mobile和jtouch中可能存在检测
发布于 2011-06-07 16:46:40
包括modernizer,它是一个小型特征检测库。然后你可以使用下面这样的东西。
if (Modernizr.touch){
// bind to touchstart, touchmove, etc and watch `event.streamId`
} else {
// bind to normal click, mousemove, etc
}
发布于 2013-04-17 18:44:48
if ("ontouchstart" in document.documentElement)
{
alert("It's a touch screen device.");
}
else
{
alert("Others devices");
}
我在到处浏览后发现的大多数简单代码
https://stackoverflow.com/questions/6262584
复制相似问题