使用Jquery检测Windows Tablet(触摸Windows设备)

我的网站运行的脚本只对鼠标有用,必须在触摸屏上禁用,因为它不适用于它们。

所以要解决它我用这个:

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 } 

但是这个代码没有检测到其他触摸设备,主要是像平板电脑(我有一个),我想用deviceAgent或其他任何其他触摸设备或所有触摸设备检测,如果可能的话。

非常感谢您的帮助 !

这应该做..我已经测试了ios safari,osx safari,windows IE,windows Chrome,windows Firefox,android。

 var isTouchDevice = 'ontouchstart' in window || (navigator.msMaxTouchPoints>0); 

基本上,我们让浏览器确定是否有触摸设备,然后查找触摸事件的浏览器特定实现。

Internet Explorer 9/10/11

 navigator.msMaxTouchPoints 

所有其他人:

 'ontouchstart' in window