首页 > 解决方案 > JQueryMobile - pagecontainerbeforeshow:从历史记录中删除上一页

问题描述

使用 jquery 和回调pagecontainerbeforeshow我想从导航历史记录中删除上一页 - 也就是说,如果用户从 page1 导航到 page2,并且用户单击后退按钮我想退出“应用程序”。

我在这里发现了同样的问题,但我不知道如何在 pagecontainerbeforeshow 中实现答案?

Jquery mobile,删除上一页

 bindEvents: function() {

    $(document).on("pagecontainerbeforeshow", function(event,ui){

        var destinationPage = ui.toPage.prop("id");
        var sourcePage = ui.prevPage.prop("id");

        d.lg("destionationPage: " + destinationPage);
        d.lg("sourcePage: " + sourcePage);
        console.log("page to be shown =" + destinationPage);
        switch(destinationPage) {
            case 'restaurants':
                d.lg('redirected to list');

            break;
            case 'offers':

                d.lg('redirected to offers');

            break;

            }
        }); 
}

标签: jqueryhtmljquery-mobile

解决方案


Bohr最终使用了另一种方法来改变后退按钮的行为

如果存在divwithid=the_id_id_of_the_page

  1. pagecontainerbeforeshow事件中,backbutton将与backButtonExit()函数相关联
  2. backButtonExit()函数将触发应用程序的退出

    navigator.app.exitApp()
    
  3. pagehidebackButtonExit功能将从中删除button并且行为将停止

    $("#the_div_id_of_the_page").on('pagecontainerbeforeshow',function(){
      document.addEventListener("backbutton", backButtonExit,false);
    })
    
    $("#the_id_id_of_the_page").on('pagecontainerhide',function(){
      document.removeEventListener("backbutton", backButtonExit,false);
    })
    
    function backButtonExit(){
       // Phonegap
       navigator.app.exitApp();
       // Normal Browser
       window.close()
    }
    

这是一个比您的解决方案更简单的解决方案,但如果您需要,它也可以集成到您的switch语句中。另外,如果您不使用phonegap,请考虑阅读这篇文章


推荐阅读