4

jQuery Mobile 1.2.0

I generate the HTML using JavaScript ($(selector).html(content)), add it to the DOM and then display it ($.mobile.changePage()).

Then I invoke an AJAX call, get some data, and re-generate the html (but the parent element, the same $(selector), stays the same, I just change its html(...)).
At this poing the HTML is not "enhanced" by jQM, no styling applied on it.

Now according to the docs I should simply call the page() function on the parent element, i.e $(selector).page().

Other places in the docs suggest triggering the create event, i.e $(selector).trigger("create").

The problem is that non of the above two methods works - the styling of jQM is not applied.

Looking at the code of jQM, I've tried triggering the pagecreate event on that element and it does work, but, this is not documented anywhere, so I'm uncertain of it, especially concerning future releases of jQM.

At some poing in the docs I've read that I can call page() on a page only once..

Anyway, is there any concise/standard way to tell jQM to "enhance" the whole element and its child-elements? Or should I simply stay with triggering the pagecreate event?

Thank you!

4

2 回答 2

1

$(selector).trigger("create"); 的作用域是什么?

您应该能够在 'pageshow' jqm 样式应用于元素之前的 'pagecreate' 事件上添加任何元素。例如,我像这样动态添加页眉/页脚

$(document).on('pagecreate', "[data-role=page]", function() {

var header = "<div data-role='header'>some header stuff</div>";
 var footer= "<div data-role='footer'>some footer stuff</div>";

$(this).prepend(header);
$(this).append(footer);

$("[data-role=header]").fixedtoolbar({tapToggle: false});
$("[data-role=footer]").fixedtoolbar({tapToggle: false});

});

确保您使用的是 jquery 1.7 或更高版本,我认为那是引入 on 方法的时候;

听起来您可能正在生成 DOM 然后更改页面,反之尝试先转到页面然后动态编辑 dom。

编辑将重新加载页面选项设置为 true

  $.mobile.changePage($(page), {reloadPage: true});

编辑 2

$(selector).children().each(function(){
   $(this).trigger('create');
})
于 2012-12-30T04:10:31.417 回答
1

要重新创建整个页面,请使用:

$(selector).trigger("pagecreate");

这是我对类似问题的回答:https ://stackoverflow.com/a/14011070/1848600 。有一个页面重建的例子。看一下,这应该可以解决你的问题。

于 2012-12-30T00:04:23.417 回答