2012-02-20 170 views
9

當訪客在圖像上點擊時,將觸發click事件。但是,當有人觸摸圖像時,即使同樣有touchstart事件可用,也會觸發相同的click事件。觸摸開始事件是否會觸發點擊事件?

我對於實際的點擊(鼠標)事件和觸摸事件喜歡不同的行爲。奇怪的是,即使是在智能手機上使用時也會觸發一個mouseup事件。無論如何,您可以將鼠標與觸摸事件分開嗎?

回答

9
event.preventDefault(); 

的伎倆,希望這可以幫助的人!

+0

如果您只是將'event.preventDefault()'添加到'touchstart'回調函數,這將不起作用。至少在iOS 8.4 Safari的鏈接上。您必須將其添加到'click'事件處理程序。 – 2015-07-09 21:57:14

1

可以歸事件..

見我回答這個問題:

Click event called twice on touchend in iPad

您還可以在jQuery Mobile的源代碼中找到靈感:http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.js開始於行982

/* 
* "events" plugin - Handles events 
*/ 

(function($, window, undefined) { 

// add new event shortcuts 
$.each(("touchstart touchmove touchend orientationchange throttledresize " + 
        "tap taphold swipe swipeleft swiperight scrollstart scrollstop").split(" "), function(i, name) { 

    $.fn[ name ] = function(fn) { 
     return fn ? this.bind(name, fn) : this.trigger(name); 
    }; 

    $.attrFn[ name ] = true; 
}); 
.... 

看的輕敲事件:(線1049)

$.event.special.tap = { 
+0

謝謝你的徹底答案。唯一的問題是,現在你在這種情況下專門針對「特殊設備」iPhone。我寧願所有支持touchstart的設備都將點擊和觸摸開始視爲不同。我希望有一個簡單的變種你可以設置禁用。 – Mark 2012-02-20 13:55:03