我的网站运行一个脚本,该脚本仅对鼠标有用,并且必须在触摸屏上禁用,因为它不适用于鼠标。
所以为了解决这个问题,我使用这个:
var deviceAgent = navigator.userAgent.toLowerCase();
var isTouchDevice = Modernizr.touch ||
(deviceAgent.match(/(iphone|ipod|ipad)/) ||
deviceAgent.match(/(android)/) ||
deviceAgent.match(/(iemobile)/) ||
deviceAgent.match(/iphone/i) ||
deviceAgent.match(/ipad/i) ||
deviceAgent.match(/ipod/i) ||
deviceAgent.match(/blackberry/i) ||
deviceAgent.match(/bada/i));
if (!isTouchDevice) {
//my fonction
}
但是这段代码不会检测其他触摸设备,主要是像 Windows 平板电脑(我有一个),我想用 deviceAgent 或其他任何东西来检测其他触摸设备,或者如果可能的话,检测所有触摸设备。
非常感谢您的帮助!
最佳答案
这应该可以。我已经在 ios safari、osx safari、windows IE、windows Chrome、windows Firefox、android 上进行了测试。
var isTouchDevice = 'ontouchstart' in window || (navigator.msMaxTouchPoints>0);
基本上,我们让浏览器确定是否存在触摸设备,然后查找触摸事件的浏览器特定实现。
Internet 浏览器 2011 年 9 月 10 日
navigator.msMaxTouchPoints
所有其他人:
'ontouchstart' in window
关于jquery - 使用 Jquery 检测 Windows 平板电脑(触摸 Windows 设备),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28860930/