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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM