Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery Mobile:获取上一页的ID_Jquery_Jquery Mobile - Fatal编程技术网

jQuery Mobile:获取上一页的ID

jQuery Mobile:获取上一页的ID,jquery,jquery-mobile,Jquery,Jquery Mobile,我基本上需要一个自定义函数,仅当从主页单击#reviews页面时使用 以下是我正在使用的当前代码: $(document).bind("mobileinit", function(){ $('#reviews').live('pagebeforeshow',function(event){ var cpage = $.mobile.activePage.attr('id'); if (cpage == 'home') { addPages(

我基本上需要一个自定义函数,仅当从主页单击#reviews页面时使用

以下是我正在使用的当前代码:

$(document).bind("mobileinit", function(){
    $('#reviews').live('pagebeforeshow',function(event){
    var cpage = $.mobile.activePage.attr('id');
        if (cpage == 'home') {
            addPages();
        }
    });
});
问题是,即使它使用的是“pagebeforeshow”,它也将要转换到的页面的id用作活动页面,在本例中是“reviews”,而不是home,从而导致if语句失败


因此,我的问题是,在本例中,如何获取上一页的id,以确保它实际上来自#主页?

pagebeforeshow
事件处理程序实际获取两个参数:事件和ui。您可以从ui访问上一页。以下是一个例子:


这些参数对于所有4个页面显示/隐藏事件都是相同的。

要完成回答:您将获得一个jQuery对象,因此要获取页面ID,请执行以下操作:

$("#page-id").live('pagebeforeshow', function(event, data) {
    console.log("the previous page was: " + data.prevPage.attr('id'));
});
数据。prevPage将上一页作为jQuery对象获取


.attr('id')正在从该jQuery对象获取id。

谢谢,这回答了我的问题,仅供其他人参考。
pagebeforeshow
事件现在已被弃用,不久将被删除。有关其更换,请参见本页:
$("#page-id").live('pagebeforeshow', function(event, data) {
    console.log("the previous page was: " + data.prevPage.attr('id'));
});