Javascript jQuery Mobile 获取当前页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12461117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 08:01:37  来源:igfitidea点击:

jQuery Mobile get current page

javascriptcordovajquery-mobile

提问by horin

I am using jQuery Mobile 1.1.1 and Apache Cordova 2.0.0. I want my app to exit when I press back button but only if current page have ID = feedZive. I am using following code to do it:

我使用 jQuery Mobile 1.1.1 和 Apache Cordova 2.0.0。我希望我的应用程序在我按下后退按钮时退出,但前提是当前页面的 ID = feedZive。我正在使用以下代码来做到这一点:

function onDeviceReady(){
    document.addEventListener("backbutton", onBackKeyDown, false);
    function onBackKeyDown(){
        if ($.mobile.activePage.is("#feedZive")){
            navigator.app.exitApp();
        }
        else{
            navigator.app.backHistory();
        }

    }
};

However it looks like I can't get the current page because I have tried the following code:

但是看起来我无法获取当前页面,因为我尝试了以下代码:

var activePage = $.mobile.activePage;
alert(activePage);

and my alert is showing undefined. I have also tried to change $.mobile.activePageto $.mobile.activePage.attr("id")but it didn't work.

我的警报显示未定义。我也尝试更改$.mobile.activePage为,$.mobile.activePage.attr("id")但没有用。

回答by howbizarre

$(document).live('pagebeforeshow', function() {
    alert($.mobile.activePage.attr('id'));
});?

http://jsfiddle.net/ajD6w/5/

http://jsfiddle.net/ajD6w/5/

回答by Hessius

Try using

尝试使用

var activePage = $.mobile.activePage.attr("id");

I made a working jsfiddle for you http://jsfiddle.net/Z5Uze/1/

我为你制作了一个有效的 jsfiddle http://jsfiddle.net/Z5Uze/1/

回答by Phill Healey

I realise this is an old thread, so this is probably a more recent addition.

我意识到这是一个旧线程,所以这可能是最近添加的。

Have you tried:

你有没有尝试过:

var activePage = $.mobile.pageContainer.pagecontainer("getActivePage");
if(activepage[0].id == yourpageid)
{ /*do something here*/ }

or another way:

或其他方式:

var activePage = $.mobile.activePage.attr('id')
if(activepage == yourpageid)
{ /*do something here*/ }

回答by mornaner

Try this, it's working for me:

试试这个,它对我有用:

var activePage = $.mobile.activePage[0].id;

Here is a Fiddle with an alert that is working: http://jsfiddle.net/9XThY/3/

这是一个带有警报的小提琴,正在工作:http: //jsfiddle.net/9XThY/3/

回答by Arun Babu

$(document).ready(function() {

//exit when back button pressed on home screen
document.addEventListener("deviceready", function() {
    document.addEventListener("backbutton", function() {
        if ($.mobile.activePage.attr('id') == "home") {
            navigator.app.exitApp();
        } else {
            navigator.app.backHistory();
        }
    }, false);
}, false);

});

});

回答by EpicPandaForce

We're doing this:

我们这样做:

$(document).on("pagecontainershow", function() {
    var activePage = $.mobile.pageContainer.pagecontainer("getActivePage");

    var activePageId = activePage[0].id;

    if(activePageId != 'splashPage') { //fix rotation splash flash bug
        $("#splashPage").hide();
    } else {
        $("#splashPage").show();
    }

    switch(activePageId) {
        case 'loginPage':
            loginPageShow();
            break;
        case 'notificationPage':
            notificationPageShow();
            break;
        case 'postPage':
            postPageShow();
            break;
        case 'profilePage':
            profilePageShow();
            break;
        case 'splashPage':
            splashPageShow();
            break;
        case 'timelinePage':
            timelinePageShow();
            break;
        default:
            break;
    }
});

And navigation works like this:

导航是这样工作的:

    $.mobile.loading('hide');
    $(":mobile-pagecontainer").pagecontainer("change", "#timelinePage", {
        transition: 'none'
    });

回答by Carter Medlin

Jquery mobile 1.4.3 friendly...

jquery mobile 1.4.3 友好...

$(document).on('pagebeforeshow', function () {
    var URL = $.mobile.path.parseUrl(window.location).toString().toLowerCase();

    alert(URL);
});

回答by horin

Both of this solutions are good but I needed to put it inside document.ready(function()

这两个解决方案都很好,但我需要把它放在 document.ready(function()

$(document).ready(function(){
var activePage = $.mobile.activePage[0].id;
alert(activePage);
});