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)不會返回錯誤的結果。
有人可以解釋一下嗎?
在布爾表達式中確實沒有多少冗餘。所有你需要的是'ontouchstart'在窗口中。現在,至於爲什麼id不起作用:我會檢查[Modernizr的功能](http://modernizr.github.com/Modernizr/touch.html)。 – Pointy 2012-02-15 14:03:05