问题
Having a very small example demonstrating my issue with jquerymobile:
JSFiddle - http://jsfiddle.net/forrest_gump/Q73Mk/3/
As you can see i call jqm changePage() and it appears for a second but the it is suddenly removed by #pageIndex
I spent a lot of time trying to find out why!?! Am I stupid or is it a jqm bug?
回答1:
Note that pageinit fires when page is ready to be shown but still hidden, as there is sequence of events that still not occurred on that page. Changing page doesn't stop those events from occuring, resulting in showing the target page for a while and reverting back to previous page.
You have two solutions for this:
Solution one:
Delay
changePageusingsetTimeout()to ensure that all page events are triggered.$(document).on('pagecreate', '#pageIndex', function () { setTimeout(function () { $.mobile.pageContainer.pagecontainer("change", "#pageLogin", { transition: "slideup" }); }, 100); /* 100ms or more */ });Note:
pageinitis deprecated and replaced withpagecreate. Also,$.mobile.changePageis replaced with$.mobile.pageContainer.pagecontainer("change", "#page", { options });.Demo
Solution two:
Listen to
pagebeforechangeevent to decide which page to show first.$(document).on("pagebeforechange", function (e, ui) { if (ui.toPage[0].id == "pageIndex" && typeof ui.options.fromPage === "undefined") { $.mobile.pageContainer.pagecontainer("change", "#pageLogin", { transition: "slideup" }); e.preventDefault(); } });Demo
来源:https://stackoverflow.com/questions/22141500/jqm-removes-my-page