我目前有一个移动网站,我正在建设和我在整合自定义JQuery小部件的麻烦。由于某种原因,页面展示事件会先发生两次,无论哪一页首先被加载。jquery移动pageshow事件发射两次
为了便于理解,我已经创建了两个HTML页面
<!doctype html>
<head>
<meta charset="utf-8">
<title>One</title>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).on("mobileinit", function() {
$.mobile.changePage.defaults.reloadPage = true;
});
</script>
<script type="text/javascript" src="https://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.js"></script>
</head>
<body id="default">
<div data-role="page" id="content-one">
<div role="main" class="ui-content" id="one-content">
One<br/>
<a href="two.html">back to two</a>
</div>
<script type="text/javascript">
(function($, undefined) {
$.widget("test.stuff", {
_create : function() {
console.log("create");
},
_init : function() {
console.log("_init");
},
destroy: function() {
this.element.empty();
$.Widget.prototype.destroy.call(this);
}
});
})(jQuery);
</script>
<script type="text/javascript">
$(document).on("pageshow", "#content-one", function() {
console.log("pageshow1");
$("#content-one").stuff({});
});
$(document).on("pagebeforeshow", "#content-one", function() {
console.log("pagebeforeshow1");
});
</script>
</div>
</body>
</html>
第2页
<!doctype html>
<head>
<meta charset="utf-8">
<title>Two</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"> </script>
<script type="text/javascript">
$(document).on("mobileinit", function() {
$.mobile.changePage.defaults.reloadPage = true;
});
</script>
<script type="text/javascript" src="https://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.js"></script>
</head>
<body id="default">
<div data-role="page" id="content-two">
<div role="main" class="ui-content">
Two<br/>
<a href="one.html">back to one</a>
</div>
<script type="text/javascript">
$(document).on("pageshow", "#content-two", function() {
console.log("pageshow2");
});
$(document).on("pagebeforeshow", "#content-two", function() {
console.log("pagebeforeshow2");
});
</script>
</div>
</body>
</html>
要重新加载页面一个第一,转到页两个使用链接,然后使用链接导航到第一页。
当页面1再次被加载时,页面显示事件被触发两次。我将reloadPage设置为true,因为我的页面具有动态内容,并且无法提供缓存页面。
我很可能会错误地使用这些事件。
'reloadPage'已被弃用,不再可以被设置为_option_。而是使用'$ .mobile.pageContainer.pagecontainer(“change”,“page.html”,{reload:true});'以加载页面并以编程方式重新加载。另外,'pageshow'已被弃用,但仍在运行,使用'$(document).on(“pagecontainershow”,function(){code});'。 – Omar
如果我不应该使用reloadPage我应该使用什么机制来确保每次加载每个页面并且不从缓存中取出?我希望这是所有导航的默认功能。 – mfleshman
'reload'而不是'reloadPage'。 – Omar