[英]mobile.changePage() not working in function
我编写了一个简单的代码来创建“启动画面”。
我正在使用localstorage
存储数据。 如果是第一次加载它,那么我将从URL中获取数据,并且可以使用$.mobile.changePage()
来更改页面。 但是,如果已经存储了localstorage
存储数据,则无法更改页面代码。
function HOME() {
if (window.localStorage.getItem('newhome')) {
$.mobile.changePage("#pageone", {
transition: "none",
changeHash: false
});
} else {
$.get('http:/someurl', function (data) { //comment this line if we make apk
window.localStorage.setItem('newhome', JSON.stringify(data));
}).done(function (data) {
$.mobile.changePage("#pageone", {
transition: "none",
changeHash: false
});
});
}
}
<body>
<div id="splash" data-role="page"><h1>splash here</h1></div>
<div id="pageone" data-role="page"><p>my content here</p></div>
</body>
如果我运行其他功能(例如console.log
,则它们可以工作,但不能运行$.mobile.changePage
。
您的页面不变的原因是,它永远不会进入if循环。 在其他执行没错,但方法.getItem()
不返回布尔类型。 要检查您的密钥是否已实际设置,应检查是否为null。 因此您的代码看起来会更像这样:
if (localStorage.getItem("username") === null) {
$.get('http://www.thejewishinsights.com/wp/wp-json/posts', function (data) { //comment this line if we make apk
window.localStorage.setItem('newhome', JSON.stringify(data));
}).done(function (data) {
$.mobile.changePage("#pageone", {
transition: "none",
changeHash: false
});
});
}
else{
$.mobile.changePage("#pageone", {
transition: "none",
changeHash: false
});
}
我没有检查代码,但您应该知道。 我也建议不要使用.changePage
因为它将按照其文档http://api.jquerymobile.com/jQuery.mobile.changePage/中所述从jQuery mobile 1.5中删除。
我希望这有帮助。 祝您新年快乐!
您是否尝试过使用此方法?
$(":mobile-pagecontainer").pagecontainer("change","#newpage");
我认为JQMobile官方网站建议使用此版本而不是您使用的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.