久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

點擊后通過 JavaScript 動態創建 jQuery Mobile 頁面

Dynamically create jQuery Mobile page via JavaScript after clicking(點擊后通過 JavaScript 動態創建 jQuery Mobile 頁面)
本文介紹了點擊后通過 JavaScript 動態創建 jQuery Mobile 頁面的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的 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() 以包含自定義加載/頁面創建代碼,但我沒有找到任何東西.或者有沒有更好的辦法解決這個問題?

解決方案

我有一些時間來解決這個問題,我找到了一個可行的解決方案(經過測試).

一些注意事項:

  1. 封裝在$(document).ready()中的javascript;用于在用戶導航到帶有哈希標記的 index.html 文件時動態創建頁面(即 index.html#some_hash_mark).
  2. 函數 create_page(page_id) 用于從鏈接創建頁面(或以編程方式,如果您愿意).
  3. 請注意,jquery 核心腳本和 jquery mobile css 在 $(document).ready() 語句之前加載,但 jquery mobile 腳本在之后加載.
  4. 查看 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:

  1. 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).
  2. The function, create_page(page_id) is for creating a page from a link (or programatically if you like).
  3. 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.
  4. 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 91在线影院 | 91精品国产91久久久久久吃药 | 在线精品一区 | 欧美精品啪啪 | 91爱爱·com | 日韩一二三区视频 | 欧美三级在线 | 免费的色网站 | 免费影视在线观看 | 欧美精品一区免费 | 国产亚洲精品久久午夜玫瑰园 | 久久精品91 | 国产精品久久久久久久久 | 中文字幕一区二区三区精彩视频 | 国产一级片免费视频 | 欧美性受xxxx白人性爽 | 亚洲成人精选 | 色综合色综合色综合 | 九九热国产视频 | 日一区二区 | 久久久精品 | 高清不卡毛片 | 欧美一级欧美三级在线观看 | 欧美视频1| 免费观看毛片 | 欧美xxxx做受欧美 | 国产精品久久久久久久久久三级 | 国产精品久久二区 | 亚洲免费在线视频 | 成人免费视频网站在线看 | 亚洲欧美日韩一区二区 | japan25hdxxxx日本 做a的各种视频 | 成人欧美一区二区三区视频xxx | 成人免费毛片在线观看 | 日韩高清中文字幕 | 午夜精品影院 | 亚洲成年影院 | 国产日韩免费视频 | 成人精品免费 | 国产精品久久久 | 亚洲精品电影网在线观看 |