jquery mobile pagecontainershow事件绑定正确的方式

时间:2014-12-01 14:50:53

标签: javascript jquery jquery-mobile

我在一个文件中有两个页面,如下所示:

    <section id="firstpage" data-role="page">
    <header data-role="header">
      <h1>Event Example</h1>
    </header>
    <div class="ui-content">
      <p>This is page 1<a href="#secondpage">click here</a></p>
    </div>
</section>

<section id="secondpage" data-role="page">
    <header data-role="header">
      <h1>Event Example</h1>
    </header>
    <div class="ui-content">
      <p>This is page2</p>
    </div>
</section>

基本上我想在第二页显示时触发pagecontainershow事件 我想要做的是当&#34; secondpage&#34;我想要执行一些初始化代码。我面临的问题是第二页在dj中没有被ajax提取,所以如何在第二页的节目上绑定事件

我做的是这个

$( document ).on( "pagecontainershow", function ( event, ui ) {
var activePage = $.mobile.pageContainer.pagecontainer( "getActivePage" );
  if(activePage[0].id=="secondpage"){
   //do the initialization for second page
  }
  });

pagecontainershow show事件是我想要的,但它会触发每个页面如何将它绑定到仅在第二页显示时触发

1 个答案:

答案 0 :(得分:0)

我可以想到两种选择,我认为它们没有任何区别,但也许值得尝试:

1)使用div而不是部分

2)使用ui对象检索当前页面而不是getActivePage方法,如下所示:

$( document ).on( "pagecontainershow", function( event, ui) {
 var pageId = ui.toPage.prop('id');
 if(pageId ==='secondpage'){
  //do the initialization for second page
 }
});