是否有任何好的、干净的方法或技巧来确定用户是否使用触摸设备?
我知道有类似的东西
var isiPad = navigator.userAgent.match(/iPad/i) != null;
但我只是想知道是否有一个技巧可以确定用户是否使用触摸设备? 因为除了 iPad 之外,还有更多的触摸设备和平板电脑。
谢谢。
最佳答案
您可以使用以下 JS 函数:
function isTouchDevice() {
var el = document.createElement('div');
el.setAttribute('ongesturestart', 'return;'); // or try "ontouchstart"
return typeof el.ongesturestart === "function";
}
来源:Detecting touch-based browsing .
请注意,上述代码仅测试浏览器是否支持触摸,而不是设备。
相关链接:
- How to detect a mobile device using jQuery
- How to optimize website for touch devices
- How to detect a mobile device using 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’
jquery for mobile中可能存在检测和 jtouch
关于javascript - 如何判断客户端是否是触摸设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6262584/