2012-11-19 206 views
2

我正在使用Javascript來處理longpress事件,它將在簡單的HTML頁面上使用時正常工作,但是當我在Phonegap/Android中使用此代碼時,它不起作用,longpress event not working in Android using Phonegap

$(document).ready(function() { 
    debugger; 
    var mousedowntime; 
    $('#Button1').mousedown(function() { 
    var d = new Date(); 
    mousedowntime = d.getTime(); 
    //alert('Handler for .mousedown() called.'); 
    //start a timer 
    }); 
    $('#Button1').mouseup(function() { 
    // debugger; 
    //alert('Handler for .mouseup() called.'); 
    //stop the timer and decide on long click 
    var d = new Date(); 
    //alert("mousedowntime=" + mousedowntime); 
    presstime = d.getTime() - mousedowntime; 
    //alert("presstime=" + presstime); 
    if (presstime > 999/*You can decide the time*/) { 
     //Do_Action_Long_Press_Event(); 
     alert("Long pressed."); 
    } else { 
     //Do_Action_Click_Event(); 
     alert("Click."); 
    } 
    }); 
}); 

回答

1

使用嘗試touchstarttouchend事件

所以:下面的代碼給出

$('#Button1').on('touchstart',function() { 
    //Logic 
}); 

$('#Button1').on('touchend', function() { 
    //Logic 
}); 
+0

嗨克拉克感謝代碼,它會正常工作。 – Gaurav

+0

沒問題。如果答案對您有用,您應該將其標記爲已接受的答案。 [更多這裏](http://stackoverflow.com/faq#howtoask) –

0

這是一個邏輯在這個SO answer。我希望這是你正在尋找的東西。