2013-01-17 167 views
-3

我创建了以下事件,Android的后退按钮(PhoneGap 2.2.0):破坏后退按钮事件 - 安卓

document.addEventListener("backbutton", function (e) { 
    //Do Something 
}, false); 

我有以下链接,带我到一个外部网站从应用

我的链接

我重写后退按钮事件,只要我通过内部应用程序到另一条链路事件没有被取消..而且因为另一个链接不知道他科尔多瓦甚至无法访问此事件。

所以我必须完全取消它!

我该怎么做......?

当我按下后退按钮我得到的日志中出现以下错误信息:

Uncaught ReferenceError: cordova is not defined at :1 

,并没有任何反应..

+1

什么是你想要做什么呢? –

+0

我想让我的活动在某个功能运行后不再工作 –

+0

如果您投下意味着是次等问题,是不是?那么为什么没有人回答它呢? –

回答

2
function onBackKey() { 
    console.log("I've caught a back key"); 

    // We are going back to home so remove the event listener 
    // so the default back key behaviour will take over 
    document.removeEventListener("backbutton", onBackKey, false); 

    // Hide the current dive and show home 
    document.getElementById(cur).style.display = 'none'; 
    document.getElementById('home').style.display = 'block';  
    cur = 'home'; 
} 

function goToDiv(id) { 
    // We are moving to a new div so over ride the back button 
    // so when a users presses back it will show the home div 
    document.addEventListener("backbutton", onBackKey, false); 

    // Hide home and show the new div 
    document.getElementById('home').style.display = 'none'; 
    document.getElementById(id).style.display = 'block'; 
    cur = id; 
} 

地方html标签

<div id="home">Back Button Home<br/><a href="javascript:goToDiv('div1')">Div One</a><br/><a href="javascript:goToDiv('div2')">Div Two</a></div> 

请查看以下链接,详细的解答

https://gist.github.com/955496

+0

谢谢谢谢谢谢,终于回答了我的问题!这是我需要的行:document.removeEventListener(“backbutton”,onBackKey,false); –

1

可以实现它作为

boolean toRun = true; 

document.addEventListener("backbutton", function (e) { 
    if (toRun)  
    { 
     //Do Something 
     toRun = false; 
    } 
}); 

设置布尔根据您的需要。并检查,如果它的第一次,执行代码。否则什么也不做。

希望它有帮助。

+0

我想完全取消该活动,这是不可能的? –

+0

@HodayaShalom,你的意思是“完全取消事件”?如果你想在特定的函数调用后取消它,你可以在该函数的最后一行中将其设置为false –

+0

我的意思是销毁这个事件。就好像他以前不存在一样。我希望Button返回到其正常操作 –