0

ユーザー エージェントがタッチ デバイスであるかどうかを調べようとしています。

if( (navigator.userAgent.match(/iPhone/i)) || 
    (navigator.userAgent.match(/iPad/i)) {
    document.addEventListener("touchstart", function() {},false);
}) 

また、Android デバイスやその他のタッチ デバイスを検出するにはどうすればよいですか?

4

2 に答える 2

0

少し前にタッチイベントをテストするために使用した簡単なスクリプトは次のとおりです。

var hasTouch = (function(){
  if (document && document.createEvent) {
    try {
      document.createEvent('GestureEvent');
      document.createEvent('TouchEvent');
      return true;
    } catch (e) {
      return false;
    }
  }
})(); 

しばらく使用していないので、もっと良い方法があるかもしれませんが、上記はまだ機能します。

また、Kangax のDetecting event support without browser sniffingに関する記事を試すこともできます。

  function isTouchEventSupported(eventName) {
    var el = document.createElement('div');
    eventName = 'on' + eventName;
    var isSupported = (eventName in el);

    if (!isSupported) {
      el.setAttribute(eventName, 'return;');
      isSupported = typeof el[eventName] == 'function';
    }
    el = null;
    return isSupported;
  }

  alert(isTouchEventSupported('touchstart'));  // false in IE 8
于 2012-05-18T02:54:07.260 に答える
0

http://modernizr.github.com/Modernizr/touch.html

これは、ブラウザ機能ライブラリ modernizr が行う方法です。それらを使用しないのはなぜですか?

于 2012-05-18T03:06:07.020 に答える