单击后通过JavaScript动态创建jQuery Mobile页面

Wit*_*tek 21 javascript navigation jquery jquery-mobile

我的jQuery Mobile应用程序由一个index.html页面组成,并且在启动时只包含一个带有链接的页面:

<div data-role="page">
  <div data-role="content">
    <a id="startPageLink" href="startPage">start</a>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

当用户点击开始链接时,我想startPage异步加载来自我的JSON api 的内容.在回调中,我想为startPagevia JavaScript 创建所有必需的DOM元素,并将内容添加到它.我createStartPage(data)为此创建了一个方法.

实现这种动态创建的页面的正确方法是什么,这样开放index.html#startPage也有效?我认为应该有一种方法来$.mobile.changePage()包含自定义加载/页面创建代码,但我没有找到任何东西.或者这个问题有更好的解决方案吗?

Jas*_*per 23

我有时间搞乱这个,我找到了一个有效的解决方案.

一些注意事项:

  1. 封装在$(document).ready()中的javascript; 用于动态创建页面,如果用户导航到index.html文件并附加了散列标记(即index.html#some_hash_mark).
  2. 函数create_page(page_id)用于从链接创建页面(如果您愿意,可以编程方式).
  3. 请注意,jquery核心脚本和jquery mobile css在$(document).ready()语句之前加载,但之后加载了jquery移动脚本.
  4. 看到body标签在向页面附加页面时被赋予了一个id.

文件样本

<!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" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.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>
Run Code Online (Sandbox Code Playgroud)

  • create_page(page_id)...它是一个从链接创建页面的功能.它创建页面,初始化它,然后将用户转发到新页面.我上面写的具体功能还在主页上创建了一个链接,因此如果用户回溯到主页,他们可以再次前进到新创建的页面.这在注释#2中记录.`<li> <a href="javascript:create_page('page_id_here');">链接文字</a> </ li> (2认同)

wez*_*zzy 5

对我来说 Jasper 解决方案不起作用,但我发现这个解决方案看起来更干净而且工作正常