[英]jQuery Mobile get current page
我正在使用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();
}
}
};
但是看起来我无法获取当前页面,因为我尝试了以下代码:
var activePage = $.mobile.activePage;
alert(activePage);
我的警报显示未定义。 我也尝试将$.mobile.activePage
更改$.mobile.activePage
$.mobile.activePage.attr("id")
但它不起作用。
$(document).live('pagebeforeshow', function() {
alert($.mobile.activePage.attr('id'));
});
尝试使用
var activePage = $.mobile.activePage.attr("id");
我为你做了一个工作jsfiddle http://jsfiddle.net/Z5Uze/1/
我意识到这是一个旧线程,所以这可能是一个更新的补充。
你有没有尝试过:
var activePage = $.mobile.pageContainer.pagecontainer("getActivePage");
if(activepage[0].id == yourpageid)
{ /*do something here*/ }
或另一种方式:
var activePage = $.mobile.activePage.attr('id')
if(activepage == yourpageid)
{ /*do something here*/ }
试试这个,它对我有用:
var activePage = $.mobile.activePage[0].id;
这是一个小提琴,有一个正常工作的警报: http : //jsfiddle.net/9XThY/3/
$(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);
});
我们这样做:
$(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;
}
});
导航的工作原理如下:
$.mobile.loading('hide');
$(":mobile-pagecontainer").pagecontainer("change", "#timelinePage", {
transition: 'none'
});
Jquery mobile 1.4.3友好...
$(document).on('pagebeforeshow', function () {
var URL = $.mobile.path.parseUrl(window.location).toString().toLowerCase();
alert(URL);
});
这两个解决方案都很好但我需要把它放在document.ready(function()中
$(document).ready(function(){
var activePage = $.mobile.activePage[0].id;
alert(activePage);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.