我是 JQM 的新手。我有文件 index.html,在其中我使用 div 标签创建了许多页面,例如
<div data-role="page" id="pg1 "></div>
现在我想使用页面 id 重新加载特定页面。如何使用 location.reload() 或其他方式来做到这一点?
谢谢..
我是 JQM 的新手。我有文件 index.html,在其中我使用 div 标签创建了许多页面,例如
<div data-role="page" id="pg1 "></div>
现在我想使用页面 id 重新加载特定页面。如何使用 location.reload() 或其他方式来做到这一点?
谢谢..
如果您使用的是多页面模板,其中所有伪页面都在一个文档中,那么默认情况下您不能刷新页面,因为它只存在于当前 DOM 中。您可以编写一个函数,在 AJAX 请求中请求相同的文档,然后从响应中获取伪页面并替换 DOM 中的当前页面:
function replace_multipage_template (SELECTOR) {
//show the loading spinner
$.mobile.showPageLoadingMsg();
$.ajax({
//request the same document as the current URL
url : window.location.href,
success : function (response) {
//select just the desired pseudo-page
var $ele = $(response).find(SELECTOR);
//replace the current pseudo-page with the new one
$(SELECTOR).replaceWith($ele);
//hide the loading spinner
$.mobile.hidePageLoadingMsg();
//navigate to the refreshed pseudo-page
$.mobile.changePage($ele);
},
error : function (jqXHR, textStatus, errorThrown) { /*make sure to handle errors*/ }
});
}
您可以在事件处理程序中将此代码click
用于将用户定向到要刷新的页面的链接的事件:
//bind via delegation to the click events for links that target a specific pseudo-page
$(document).delegate('a[href="#some-page-id"]', 'click', function () {
//call the function from above, using the HREF attribute as the SELECTOR variable
replace_multipage_template($(this).attr('href'));
//prevent the default behavior of the link
return false;
});
http://jquerymobile.com/test/docs/api/methods.html
使用带有 reloadPage 选项的 $.mobile.changePage 方法