在您的情况下,您需要收听pagebeforechange
事件以通过显示data.toPage
并显示另一个。
使用pagebeforehide
andpagebeforeshow
将导致显示data.toPage
然后跳转到目标页面。
演示
// for demo purposes
var condition = 0;
// triggers when leaving any page
$(document).on("pagebeforechange", function (e, data) {
// id of page that you want to skip if condition is true/false, it's up to you
var to_page = data.toPage[0].id;
// skip showing #myPage if condition is true
if (to_page == "myPage" && condition == 0) {
// true! go to p3 instead
$.mobile.changePage("#p3", {
transition: "flip"
});
/* prevent updating URL history and allow new transition if you want.
Without this, #myPage will be pushed into $.mobile.urlHistory
and any new transition (animation) will be neglected */
e.preventDefault();
}
/* in the demo, if you change condition value, #p2 will be shown
when condition returns "false". #myPage will be shown normally */
});
注意: pagebeforechange
会触发两次,这是正常的,不要惊慌;)