2014-10-06 79 views
0

我有這樣的代碼:爲什麼點擊事件被touchstart事件抑制?

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

只有touchstart被觸發。有人可以解釋爲什麼嗎?

P.S. :這是我包括jQuery Mobile的腳本: <script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>

編輯: 我想在我的DIV的一個介紹一個懸停功能,我認爲用自來水事件就會像單擊並像懸停touchstart 。

$('.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輕掃事件工作正常,但對於內部元件我無法重現刷卡事件,僅自來水被觸發。我真的不明白爲什麼。

回答

0

這樣使用語法:

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

     alert(3); 

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

     alert(4); 

    } 
    e.stopPropagation(); 
}); 
+0

即使我點擊我的div,第一個觸發的事件是touchstart,所以只會顯示警報3。我想分開touchstart和tap的行爲。 – 2014-10-06 09:32:59

0

Touchstart似乎不再提供爲jQuery Mobile的版本內的本地事件。 1.4.5也許你可以使用「點擊」和「滑動」(或「swipeleft」/「swiperight」)來實現你的目標?它不完全清楚你正在創建什麼。

注意:您可以做的一件事就是輕鬆定製原生jQuery Mobile功能進行滑動。詳細信息請參見http://api.jquerymobile.com/swipe/

在創建.on(「swipe」...)綁定之前,您可以更改jQuery swipe使用的測試值。它簡單易行

$.event.special.swipe.horizontalDistanceThreshold = 100; // in lieu of 30px 

您可以通過警報或console.log()驗證數據設置。也許這會對你有用?嗯..通過在垂直距離閾值上工作,您實際上可以臨時將「滑動」定義爲「向上滑動」,然後您可以使用滑動,swiperight和滑動來進行遊戲。