Jquery Mobile中pageinit等函数执行两次的问题【终极解决】

当禁用了jqueryMobile的ajax后,初始化函数如pageinit和pageshow等函数,都会执行两次。document.ready函数也会执行两次。

当然我们可以用一个变量记录是否已经执行,如果已经执行就不再执行第二次,但终究这不是最终办法。

    var loaded = false;//防止执行重复

    $(document).ready(function () {
        if (!loaded) {

            //do something
             loaded = true;
        }
    });

    $(document).live('pageshow', function () {

        if (!loaded) {

            //do something
            loaded = true;
        }
    })

ps:jqM是强烈建议,把原来的ready函数换成pageinit函数。
解决避免执行两次的办法是:在body中加如data-role="page",标记当前文档是page对象。

jqm中一个document中有多个page对象。当然也可以将div标记为page对象。

 

以下是原文:

The docs say very prominently not to use "$(document).ready()" any more but to use "pageInit()" instead.
So I am trying to change to pageInit() and also change from "window.open" to "pageChange".
 
I understand that to do this all pages should have the same header that references all the sources needed by any of the pages.
And I understand the page specific code has to be in the <div data-role="page"> element.
posted @ 2014-08-15 15:28  二师弟tl  阅读(4031)  评论(0编辑  收藏  举报