jquery mobile更改为下一个和上一个data-role =页面

时间:2013-07-09 11:02:28

标签: javascript jquery html html5 jquery-mobile

我在我的项目中使用jquery mobile而不是尝试使用滑动效果,使用两个按钮更改为下一个和上一个data-role =页面。

我正在尝试使用这个javascript,但我不知道为什么不工作

这是任何帮助。

HTML:

<div data-role="page" id="p1">
 <div data-role="header" data-theme="a" data-position="fixed" data-id="footer" data-fullscreen="true">                  
 <a href="#prvbutton" id="goback" data-icon="arrow-l" style="margin-right:340px" class="ui-btn-right" data-inline="true" data-iconpos="notext" data-direction="reverse">Página Anterior</a>
<a href="#nextbutton" id="goforward" data-icon="arrow-r" style="margin-right:290px" class="ui-btn-right" data-inline="true" data-iconpos="notext" data-direction="reverse">Próxima Página</a>
</div>
<div data-role="content">
.....something......
</div>
</div>

<div data-role="page" id="p2">
 <div data-role="header" data-theme="a" data-position="fixed" data-id="footer" data-fullscreen="true">                  
 <a href="#prvbutton" data-icon="arrow-l" style="margin-right:340px" class="ui-btn-right" data-inline="true" data-iconpos="notext" data-direction="reverse">Página Anterior</a>
<a href="#nextbutton" data-icon="arrow-r" style="margin-right:290px" class="ui-btn-right" data-inline="true" data-iconpos="notext" data-direction="reverse">Próxima Página</a>
</div>
<div data-role="content">
.....something......
</div>
</div>

依旧........

使用Javascript:

$("#nextbutton").on('click', '#goforward', function () {
 var next = '#' + $.mobile.activePage.next('[data-role=page]')[0].id;
 $.mobile.changePage(next, {
    transition: 'slide'
 });
});

// Previous page
$("#prvbutton").on('click', '#goback', function () {
 var prev = '#' + $.mobile.activePage.prev('[data-role=page]')[0].id;
 $.mobile.changePage(prev, {
    transition: 'slide',
    reverse: true
 });
});

2 个答案:

答案 0 :(得分:4)

给出的答案是正确的,但是,首先需要检查活动页面之前或之后是否存在现有页面。因为如果您在$.mobile.changePage()值上调用undefined,则两个按钮都将停止工作。

  

<强> Demo

$(document).on('click', '#goforward', function () {
  if ($.mobile.activePage.next('.ui-page').length !== 0) {
   var next = $.mobile.activePage.next('.ui-page');
   $.mobile.changePage(next, {
       transition: 'slide'
   });
  } 
});

$(document).on('click', '#goback', function () {
  if ($.mobile.activePage.prev('.ui-page').length !== 0) {
   var prev = $.mobile.activePage.prev('.ui-page');
   $.mobile.changePage(prev, {
       transition: 'slide',
       reverse: true
   });
  }
});

答案 1 :(得分:2)

您使用的选择器错误,您将hrefid混合在一起。更改您的选择器以匹配您的HTML代码。

$(document).on('click', '#goforward', function () {
    var next = '#' + $.mobile.activePage.next('[data-role=page]')[0].id;
    $.mobile.changePage(next, {
        transition: 'slide'
    });
});

// Previous page
$(document).on('click', '#goback', function () {
    var prev = '#' + $.mobile.activePage.prev('[data-role=page]')[0].id;
    $.mobile.changePage(prev, {
        transition: 'slide',
        reverse: true
    });
});