问题描述
我正在开发我的第一个jQuery Mobile项目并遇到了障碍。我使用的是1.0版,到目前为止,该项目非常简单,只有索引上有三个脚本。我错误地使用了 $(document).ready(function(){...});
,然后在文档中读取正确的事情是bind pageinit
或 pagecreate
。以下是我正在进行测试的脚本(直接从文档中获取):
I am working on my first jQuery Mobile project and have encountered a roadblock. I am using Version 1.0 and so far the project is very simple, three pages with script only on the index. I incorrectly used $(document).ready(function() { ... });
at first and then read in the documentation that the correct thing to do is bind pageinit
or pagecreate
. Here is the script I am working with for testing purposes (straight off of the docs):
$("#main").live('pageinit', function(event) {
alert( 'This page was just enhanced by jQuery Mobile!' );
});
我已经尝试过将此作为外部JS文件和中的脚本#main
DIV,正如我在这里发现的一些旧线程中所建议的那样。这在第一次加载索引时有效,但是当用户导航到另一个页面然后返回(重新访问)时,它不会再次触发。
I have tried this both as an external JS file and a script within the #main
DIV, as suggested in some older threads I've found on here. This works when the index is loaded the first time, but when the user navigates to another page and then returns (revisits), it does not fire again.
我是什么缺少?
推荐答案
pageshow
每次显示页面时都会触发,在初始页面加载时或用户导航离开然后导航回该页面。
pageshow
will fire each time a page is shown, either on the initial page-load or when the user navigates away and then navigates back to that page.
以下是使用 pageshow 在页面的每个视图上运行代码:(请注意,此代码在结束
< / body>
标记之前运行,并且未封装在 $中(document.ready(function(){...});
function)。
Here is an example of using
pageshow
to run code on each view of a page: http://jsfiddle.net/jasper/DHeva/ (Note that this code is being run just before the closing </body>
tag and is not encapsulated in a $(document.ready(function () {...});
function).
这是jQuery Mobile Events的文档链接(这清楚地解释了他们每个人何时开火):(大约一半的页面会启动页面事件)。
Here is a documentation link for jQuery Mobile Events (which clearly explain when each of them fires): http://jquerymobile.com/demos/1.0/docs/api/events.html (about half-way down the page starts the page events).
这篇关于jQuery Mobile - pageinit和pagecreate只触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!