問題描述
我的 jQuery Mobile 應用程序由一個 index.html
頁面組成,并且只包含一個在啟動時帶有鏈接的頁面:
<div data-role="page"><div data-role="內容"><a id="startPageLink" href="startPage">開始</a></div></div>
當用戶點擊開始鏈接時,我想從我的 JSON api 異步加載 startPage
的內容.在回調中,我想通過 JavaScript 為 startPage
創建所有必需的 DOM 元素并將內容添加到其中.我為此創建了一個 createStartPage(data)
方法.
實現這種動態創建的頁面的正確方法是什么,以便打開 index.html#startPage
也有效?我認為應該有一種方法可以連接到 $.mobile.changePage()
以包含自定義加載/頁面創建代碼,但我沒有找到任何東西.或者有沒有更好的辦法解決這個問題?
我有一些時間來解決這個問題,我找到了一個可行的解決方案(經過測試).
一些注意事項:
- 封裝在$(document).ready()中的javascript;用于在用戶導航到帶有哈希標記的 index.html 文件時動態創建頁面(即 index.html#some_hash_mark).
- 函數 create_page(page_id) 用于從鏈接創建頁面(或以編程方式,如果您愿意).
- 請注意,jquery 核心腳本和 jquery mobile css 在 $(document).ready() 語句之前加載,但 jquery mobile 腳本在之后加載.
- 查看 body 標記已被賦予一個 id,該 id 在向其附加頁面時被引用.
文檔示例
My jQuery Mobile app consists of a single index.html
page and contains only one page with a link on startup:
<div data-role="page">
<div data-role="content">
<a id="startPageLink" href="startPage">start</a>
</div>
</div>
When the user clicks on the start link, I want to load the content for the startPage
from my JSON api asynchronously. On the callback I would like to create all the required DOM elements for startPage
via JavaScript and add the content to it. I have created a createStartPage(data)
method for this.
What is the right way to implement such dynamically created pages, so that opening index.html#startPage
also works? I think there should be a way to hook into $.mobile.changePage()
to include custom loading/page-creation code, but I did not find anything. Or is there a better solution for this problem?
解決方案 I had some time to mess around with this and I've found a solution that works (tested).
SOME NOTES:
- the javascript encapsulated in $(document).ready(); is for dynamically creating a page if the user navigates to your index.html file with a hash mark already appended (i.e. index.html#some_hash_mark).
- The function, create_page(page_id) is for creating a page from a link (or programatically if you like).
- Note that the jquery core script and the jquery mobile css are loaded before the $(document).ready() statement but that the jquery mobile script is loaded after.
- See that the body tag has been given an id that is refrenced when appending pages to it.
Document Sample
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" />
<script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
//check if hash exists and that it is not for the home screen
if (window.location.hash != '' && window.location.hash != '#page_0') {
//set whatever content you want to put into the new page
var content_string = 'Some ' + window.location.hash + ' text...<br><br><a href="#page_0">go to home screen</a>';
//append the new page onto the end of the body
$('#page_body').append('<div data-role="page" id="' + window.location.hash.replace('#','') + '"><div data-role="content">' + content_string + '</div></div>');
//add a link on the home screen to navaigate to the new page (just so nav isn't broken if user goes from new page to home screen)
$('#page_0 div[data-role="content"]').append('<br><br><a href="#' + window.location.hash.replace('#','') + '">go to ' + window.location.hash.replace('#','') + ' page</a>');
}
});
function create_page(page_id) {
//set whatever content you want to put into the new page
var string = 'FOO BAR page...<br><br><a href="#page_0">return to home screen</a>';
//append the new page onto the end of the body
$('#page_body').append('<div data-role="page" id="' + page_id + '"><div data-role="content">' + string + '</div></div>');
//initialize the new page
$.mobile.initializePage();
//navigate to the new page
$.mobile.changePage("#" + page_id, "pop", false, true);
//add a link on the home screen to navaigate to the new page (just so nav isn't broken if user goes from new page to home screen)
$('#page_0 div[data-role="content"]').append('<br><br><a href="#' + page_id + '">go to ' + page_id + ' page</a>');
//refresh the home screen so new link is given proper css
$('#page_0 div[data-role="content"]').page();
}
</script>
<title>Fixed Headers Example</title>
<script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
</head>
<body id="page_body">
<div data-role="page" id="page_0">
<div data-role="content">Some #page_0 text...<br><br><a href="javascript: create_page('foo_bar_page');">create new page</a></div>
</div>
</body>
</html>
這篇關于點擊后通過 JavaScript 動態創建 jQuery Mobile 頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!