0

私はこのコードを持っています:

$('.selector').on({
        touchstart: function (e) {
          e.preventDefault();
          alert(3);
          e.stopPropagation();
        },
        tap: function (e) {
          e.preventDefault();
          alert(4);
          e.stopPropagation();
        }
      });

タッチスタートのみがトリガーされます。誰でも理由を説明できますか?

PS:これは、jQueryモバイルのスクリプトを含める方法です: <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

編集:divの1つにホバー機能を導入したいのですが、タップイベントではクリックのようになり、タッチスタートではホバーのようになると思いました。

$('.selector').on('tap swipe', function (e) {
        e.preventDefault();
        if(e.type == 'swipe') {
          alert(1);
        }
        if(e.type == 'tap') {
          alert(2);
        }
        e.stopPropagation();
      });

      $('.selector .sel1').on('tap swipe', function (e) {
        e.preventDefault();
        if(e.type == 'swipe') {
          alert(3);
        }
        if(e.type == 'tap') {
          alert(4);
        }
        e.stopPropagation();
      });

このコードでは、div のスワイプ イベントは正常に動作しますが、要素内ではスワイプ イベントを再現できず、タップのみがトリガーされます。理由が本当にわかりません。

4

2 に答える 2

0

次のような構文を使用します。

$('.selector').on("touchstart tap", function(e){
   e.preventDefault();
   if(e.type == "touchstart"){

      alert(3);

   }
   if(e.type == "tap"){

      alert(4);

   }
   e.stopPropagation();
});
于 2014-10-06T08:20:09.697 に答える