我正在使用这个函数来检测设备是否是触摸设备:
function is_touch_device()
{
return !!('ontouchstart' in window) || !!('onmsgesturechange' in window);
};
从这里得到这个函数:What's the best way to detect a 'touch screen' device using JavaScript?
但自从 Chrome 25 (25.0.1364) 以来,它在我的桌面上返回 true,这不是触摸设备。 另外我已经将 IE9 更新到 IE10,它在 IE 中返回 true!
四处搜索但找不到任何有用的方法来解决这个问题,除了使用像这样的东西:http://detectmobilebrowsers.com/
你有什么建议?
期待您的回复!
最佳答案
代码运行良好,浏览器能够理解触摸事件,仅仅因为您的屏幕不可触摸并不意味着浏览器不支持该功能。您要测试的是一种无法真正测试的硬件功能。您始终可以使用不同的方式来查看用户是否在触摸一次触摸界面后实际使用它,例如 this文章描述,或大型图书馆使用的许多其他内容,例如 Modernizer。
作为引用,上面文章中实际使用的代码是:
function isTouchDevice() {
var el = document.createElement('div');
el.setAttribute('ongesturestart', 'return;');
if(typeof el.ongesturestart == "function"){
return true;
}else {
return false
}
}
关于Javascript,检测触摸设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15221680/