我只有一個頁面,關鍵是要有pagebeforechange在您的文檔準備或ondeviceready功能。
$(document).bind("pagebeforecreate",function(e, data){
if (typeof data.toPage === 'string'){
var p = $.mobile.path.parseUrl(data.toPage);
if(u.hash.search(new RegExp(/^MyPageName/)) !== -1) {
$.mobile.showPageLoading();
MyCodeToExecForThisPage(p, data.options);
e.preventDefault(); // tell jquery i will create a page not him
}
}
});
因此多數民衆贊成你的方式,你現在需要一個函數來處理頁面,並顯示HTML,這裏是我做的:
function SetPagesTemplate(){
$page = $("#AllPage"); // this is the name of my page in the index file
$header = $page.children("#AllHead"); // my header so i can custom change
$content = $page.children(":jqmData(role=content)"); // the content area of page
$header.html('custom graphic or text');
$content.html(''); // clear everything up
$footer = $page.children("#allFoot"); // hook into my footer bar
$footer.html('custom footer nav and links');
}
下面
導航到一個頁面時,代碼顯示那內斯#person?PERSONID = 22的值。例如我的頁面的人,我將採取的ID [PERSONID]和獲取正確的信息,我會再填充我所有頁頁面與內容,使jQuery的認爲我實際上是在#人。
// take note here that i take in urlObj [ this was p ] and options
function LoadPerson(urlObj, options) {
try{
// lets get the number sent by stripping everything out
var id = urlObj.hash.replace(/.*personid=/,""),
extra = "";
SetPagesTemplate(); // the code that sets the page so i dont have to repeat myself.
$.ajax({
url: "http:\/\/" + domain + "/Ajax/MobilePersonProfile",
type: "POST",
dataType: "json",
data: { id: id },
crossDomain: true,
success: function (data) {
//loop through data create rows
if(data.length === 0){ ThrowCustomAlert('Sorry, unable to load profile.', 'no profile available', true); }
else {
// now deal with your data on this example your processed data would be called template
// all this page stuff was set up earlier so lets fill it
$content.append("<section><nav id='profilescroll'>" + extra + template + "</nav></section>");
$page.page(); // make our page into a page
options.dataUrl = urlObj.href;
template = null;
data = null;
// above cleanup, on mobile device to keep memory leaks low
// now we navigate to our created page THIS
$.mobile.changePage($page, options);
StartScroller("profilescroll");
HideLoading(); // lets hide the page loading
}
},
error: function (jqXHR, textStatus, errorThrown) { CustomError(errorThrown,'',"Profile",true); }
});
}
catch(ee){ CustomError(ee.message, ee.description, "profile", true); }
}
這就是你所需要的,它的一切我曾經使用過,我不認爲我已經看到的例子是這樣,我不得不基本上把我的整個應用程序進入工作,以減少內存的這種方式在使用iPhone 2作爲phonegap應用程序時的使用情況,使用完整的ajax導航非常快速。
點要注意的是,你只需要包括hashtag導航,內容pagebeforecreate建立一個新的,如果每一頁你想要的。 w^
也希望它可以幫助
這裏要求的是我的頁面的HTML:你使用的是什麼版本的JQM的
<div data-role="page" id="AllPage" class="body" style="overflow:hidden;">
<header class="bar" id="AllHead"></header>
<div data-role="content" class="content" id="home"></div><!-- /content -->
<footer class="bar" id="allFoot"></footer>
</div><!-- /page -->
我能問嗎? –