嗨,我正在構建一個web應用程序。要去除 http://cubiq.org/remove-onclick-delay-on-webkit-for-iphone 的onclick延遲,我發現這個腳本的代碼是bascically-刪除android的web應用程序的onclick延遲
function NoClickDelay(el) {
this.element = el;
if('ontouchstart' in window){
console.log("===================touch supported :P")
this.element.addEventListener('touchstart', this.handleEvent, false);
}
}
NoClickDelay.prototype = {
handleEvent: function(e) {
switch(e.type) {
case 'touchstart': this.onTouchStart(e); break;
case 'touchmove': this.onTouchMove(e); break;
case 'touchend': this.onTouchEnd(e); break;
}
},
onTouchStart: function(e) {
//e.preventDefault(); //removed to let the page scroll
this.moved = false;
this.element.addEventListener('touchmove', this, false);
this.element.addEventListener('touchend', this, false);
},
onTouchMove: function(e) {
this.moved = true;
},
onTouchEnd: function(e) {
this.element.removeEventListener('touchmove', this, false);
this.element.removeEventListener('touchend', this, false);
if(!this.moved) {
// Place your code here or use the click simulation below
var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
var theEvent = document.createEvent('MouseEvents');
theEvent.initEvent('click', true, true);
theTarget.dispatchEvent(theEvent);
}
}
};
我的問題是,這適用於iPhone/iPad的,但不是在Android上。什麼阻止它在Android中工作,我能做些什麼來實現類似的行爲,在Android和其他設備?請幫忙。
是的。真正的問題是增加e = e.originalEvent;在onTouchEnd函數中使用 。 :) – ghostCoder 2012-03-28 11:22:33