Dynamically creating jQuery Mobile pages using jQu

2020-02-09 03:14发布

I am building a workout catalog using jquery mobile for the UI and jquery templates to deal with the html. I have been able to append html to an already created page AND get jquery mobile to change the markup thanks to the .page() function.

However, I want to be able to create new jq mobile pages. I can inject code into divs with data-role=page, call .page() on it and it's all fine. But appending a fully made page to the body does not work.

EDIT: This question and my answer refers to jquery mobile alpha 3

3条回答
相关推荐>>
2楼-- · 2020-02-09 03:45

Gravedigging this thread. Using the jquery mobile (1.1.0), this was working for me, .trigger("create") ...

    content = '<div data-role="page" id="myID" data-url="myID">'
    // ...
    $('body').append(content).trigger("create")
    $("<a href='#myID' data-rel='dialog'/>").trigger("click")
查看更多
Fickle 薄情
3楼-- · 2020-02-09 03:57

Ok I got it. If you want to add a to the DOM you have to also add a value for data-url. When you create a static html page,

<div data-role="page" id="home">

jQuery mobile automatically adds a data-url equal to the id you give it. When you do it yourself, you have to hold its hand and pass it a data-url="home".

When you generate your html string append it to $.mobile.pageContainer so jQuery Mobile knows where to find it (appending it to the body works also, but it's probably best not to rock the boat). After that, call .page() on your page in the DOM so that jQuery mobile does all of its magical <span> magic to make it pretty.

$('#home').page();
查看更多
▲ chillily
4楼-- · 2020-02-09 04:03

when you call $('#home').page(); you're asking jQuery to enhance your div with the page's specific stylesheet and the js functions. The page is already present in the DOM, but to show it you must call $.mobile.changePage("#home",options). for more information (and object-specific options), see http://jquerymobile.com/test/docs/api/methods.html

查看更多
登录 后发表回答