我已經問了一個類似的問題,但沒有得到真正令人滿意的答案,所以我會在這裏再試一次。當我使用這樣的代碼:e.preventDefault()不是防彈的?
$("#element") .on('click', function() {
// do something
})
.on('touchstart', function(e) {
e.preventDefault();
// do the same thing but on touch device like iPad f.e.
// and more over PLEASE do not fire the click event also!
});
我期待的是,點擊事件是永遠永遠永遠開除(因爲它的瀏覽器應通過事件的工作層次),但事實上,當我觸摸元素讓我們說20次,比單擊事件還會發生一次。爲什麼?
而且我也嘗試了諸如:
$("#element") .on('click', function() {
// do something
})
.on('touchstart', function(e) {
e.preventDefault();
e.stopPropagation();
// do the same thing but on touch device like iPad f.e.
// and more over PLEASE do not fire the click event also!
return false;
});
這似乎是有點更加堅實但不防彈。 所以我不得不這樣做:
$("#element") .on('click', function() {
if (!touchdevice) {
// do something
};
})
.on('touchstart', function(e) {
e.preventDefault();
e.stopPropagation();
// do the same thing but on touch device like iPad f.e.
// and more over PLEASE do not fire the click event also!
return false;
});
這似乎是工作,但就是非常愚蠢的,是不是?任何人有想法這是關於什麼?謝謝!
編輯: 所以,我真的不得不這樣做:
var touched;
$("#element") .on('click', function() {
if (!touched) {
// do something
};
})
.on('touchstart', function(e) {
e.preventDefault(); // actual senseless here
e.stopPropagation(); // actual senseless here
touched = true;
// do the same thing but on touch device like iPad f.e.
// and more over PLEASE do not fire the click event also!
return false; // actual senseless here
});
來吧傢伙!必須有一個安全的通用方法來在同一個元素上使用touchstart和click事件,以確保點擊事件不會觸發(兩次)以處理觸摸敏感和常規瀏覽器(或者甚至是新的組合觸發器)。你會怎麼做?
我認爲這可能與http://stackoverflow.com/questions/27225577/how-to-prevent-ghost-clicks-on-hyperlinks-when-tapping-on-touch-device嘗試查看是否在點擊元素外部時發生「點擊」。 – aKzenT 2014-12-02 22:35:41
感謝您的評論!看到我自己的答案,看看我到目前爲止做了什麼。 – Garavani 2014-12-04 07:47:55