2012-02-15 110 views
3

根据 What's the best way to detect a 'touch screen' device using JavaScript? 我实现使用触摸屏检测:铬17

((!!('ontouchstart' in window) ? 1 : 0) == 1) 

此语句很好地返回true或false,同样在最新的Chrome浏览器(V17)的触控装置检测;正如所述。 为了美化我的代码了一下,一个把这个语句转换成一个小功能

function isTouchDevice() { 
return ((!!('ontouchstart' in window) ? 1 : 0) == 1); 
}; 

遗憾的是此功能错误地在Chrome 17返回TRUE,而(如上stateted),语句本身工作正常,如果调用在线或直接从控制台。

其他浏览器(IE,FF)不会返回错误的结果。

有人可以解释一下吗?

+3

在布尔表达式中确实没有多少冗余。所有你需要的是'ontouchstart'在窗口中。现在,至于为什么id不起作用:我会检查[Modernizr的功能](http://modernizr.github.com/Modernizr/touch.html)。 – Pointy 2012-02-15 14:03:05

回答

0

我在Chrome 17上,我弄错了。 (OS X 10.7.3)