How to initialize pages in jquery mobile? pageinit not firing

后端 未结 11 1216
死守一世寂寞
死守一世寂寞 2020-11-29 01:44

What\'s the right way to initialize objects on a jquery mobile page? The events docs say to use \"pageInit()\" with no examples of that function, but give exam

相关标签:
11条回答
  • 2020-11-29 01:58

    It started working when I embedded script within page div:

    <body>
        <div id="indexPage" data-role="page">
            <script type="text/javascript">
                $("#indexPage").live('pageinit', function() {
                    // do something here...
                });
            </script>
        </div>
    </body>
    

    Used jQuery Mobile 1.0RC1

    0 讨论(0)
  • 2020-11-29 02:02

    pageinit will not fire in case it is on secondary pages ( NOT MAIN page ) if it is written in common <script> tag...

    I have such a problem - on secondary pages that are not loaded with 'rel="external"', the code in the common <script> tag is never executed...

    really this code is always executed...

    <body>
        <div id="indexPage" data-role="page">
            <script type="text/javascript">
                $("#indexPage").live('pageinit', function() {
                    // do something here...
                });
            </script>
        </div>
    </body>
    

    althrough if you made "tabbed interface", using of "pageshow" is better

    0 讨论(0)
  • 2020-11-29 02:02

    @Wojciech Bańcer

    From the jQuery docs:

    As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
    
    0 讨论(0)
  • 2020-11-29 02:03

    The easiest way I found to deal with this was to use JQM + Steal. It works like a charm as long as you put:

    <script type='text/javascript' src='../steal/steal.js?mypage'></script>
    

    Inside of the data-role='page' div.

    Then use AJAX to connect anything that can use the same mypage.js and use an external link (by using the rel="external" tag) to anything that requires a different steal page.

    0 讨论(0)
  • 2020-11-29 02:07

    .live() is deprecated, suggestion is to use .on() in jQuery 1.7+ :

    <script type="text/javascript">
        $(document).on('pageinit', '#indexPage',  function(){
            // code 
        });
    </script>
    

    Check the online doc for more information about .on(): http://api.jquery.com/on/

    0 讨论(0)
  • 2020-11-29 02:08
    $(document).on("pageinit", "#myPage", function(event) {
       alert("This page was just enhanced by jQuery Mobile!");
    });
    
    0 讨论(0)
提交回复
热议问题