2

プロジェクトでjqueryモバイルを使用していますが、スワイプ効果を使用する代わりにやろうとしていることは、2つのボタンを使用して次と前のデータロール=ページに変更することです。

このJavaScriptを試していますが、なぜ機能しないのかわかりません

任意の助けのためのthx。

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
 });
});
4

2 に答える 2

4

与えられた答えは正しいですが、最初に、アクティブなページの前後に既存のページがあるかどうかを確認する必要があります。値を呼び出す$.mobile.changePage()undefined、両方のボタンが機能しなくなるためです。

デモ

$(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
   });
  }
});
于 2013-07-09T13:23:02.377 に答える
2

使用しているセレクターが間違っています。 と が混在しhrefていidます。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
    });
});
于 2013-07-09T11:31:39.833 に答える