【问题标题】:Javascript function not being called when I navigate to a page in jQuery Mobile当我导航到 jQuery Mobile 中的页面时未调用 Javascript 函数
【发布时间】:2013-05-30 12:05:51
【问题描述】:

我有一个简单的 jQuery Mobile 应用程序,基本上只有 2 个页面。

第一页只有一个按钮:

  <a href="location.html" data-theme="b" data-role="button" data-icon="arrow-r">Create Report</a>

当点击按钮时,我导航到location.html,这个页面有它自己的 JS 文件,里面有以下内容:

(function( $, undefined ) { 
    $( document ).on( "pageshow", function(){
        initMap();
    });
})( jQuery );

但是,当我到达页面时,不会调用 JS 函数 initMap();,如果我刷新页面,则会调用该函数,但在我使用主页上的按钮导航时不会调用该函数。

如何确保每次导航到此页面时都会调用此函数?

【问题讨论】:

    标签: jquery jquery-mobile


    【解决方案1】:

    我希望你有一个location.html 页面的 ID。如下所示

    <div data-role="page" id="location">
    </div>
    

    如下修改你的js文件内容

    $(document).on('pageshow', '#location', function(){
       initMap();
    });
    

    现在这应该在您每次访问页面时调用该函数。

    【讨论】:

      【解决方案2】:

      你为什么不试试这个

      pageScript(function($context){
        $context.bind("pagecreate", function(event, ui) {
          initMap();
        });
      });
      

      在这里查看更多功能

      http://markdalgleish.com/2011/04/document-ready-for-jquery-mobile/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多