jquery - 如何检测后退按钮事件?

标签 jquery cordova jquery-mobile

打开navigator.notification.confirm时如何检测后退按钮事件?返回按钮只是关闭弹窗,但是事件 document.addEventListener('backbutton', onBackKeyDown, false);没有提出。

最佳答案

在传递给 navigator.notification.confirm API 的 success 回调中,您可以获取单击了哪个按钮的buttonIndex。它没有记录,但如果 buttonIndex=0,则用户要么单击对话框外部以将其关闭,要么单击“后退”按钮。

例如:

  function makeConfirm(){
        navigator.notification.confirm(
            'You are the winner!',  // message
            onConfirm,              // callback to invoke with index of button pressed
            'Game Over',            // title
            'Restart,Exit'          // buttonLabels
        );        
    }

    function onConfirm(buttonIndex){
        console.log("confirmation! Button clicked was:" + buttonIndex);
        if( buttonIndex===0 ){
            // they either hit back button or tapped the area outside of the dialog
        }
    }

我不确定是否有办法确定他们是单击了实际的硬件后退按钮还是只是单击了对话框。您可以在页面主体上注册一个点击事件,看看是否触发了该事件,如果触发了,则 buttonIndex===0,然后他们在对话框外部单击。如果 buttonIndex===0 但它没有被触发,他们就会点击硬件后退按钮。

我还阅读了 StackOverflow 上有关使用 JQM 检测后退按钮是否被按下的其他问题;也许你可以用这个。

关于jquery - 如何检测后退按钮事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16694083/

相关文章:

JavaScript 函数中的匿名函数行为异常

jquery - 如何使jquery mobile中输入文本的方向从右到左?

cordova - Facebook JS SDK 可以与 Phonegap/Cordova 一起使用吗?

javascript - phonegap 中的文件选择器支持 ios 和 Android

html - 带有垂直 slider 的 jquery mobile

css - jQuery Mobile 为主题加载一组特定的图标?

jquery - 流量比较条形图

javascript - 是否有其他选项可以包含 javascript 中的 'input' 元素而不使用单引号?

javascript - 如何在没有 css 的情况下检查鼠标是否在屏幕上的 "invisible"框内(JavaScript 和 jQuery)

javascript - 如何从 android -Cordova/Phonegap 调用 javascript 函数