2

私はこのようなことをしたい:

$(document).on("pagebeforeshow", "#myPage", function(event){ 
  if(condition){
    //I need to do something here to stop loading "myPage"
    ???
    $.mobile.changePage("example.jsp");
   }
   else {
     //continue showing actual page normally
      ...
   }
});

「myPage」を一瞬表示せずに「example.jsp」ページに変更する方法が見つかりませんでした。それを行う方法はありますか?window.stop()andのようなものを使ってみevent.stopPropagation()ましたが、うまくいきません。

ありがとうございました!

4

4 に答える 4

3

あなたの場合、pagebeforechangeイベントをリッスンして表示を渡しdata.toPage、別のイベントを表示する必要があります。

pagebeforehideandを使用するとpagebeforeshow、表示data.toPageされ、目的のページにジャンプします。

デモ

// for demo purposes
var condition = 0;

// triggers when leaving any page
$(document).on("pagebeforechange", function (e, data) {

  // id of page that you want to skip if condition is true/false, it's up to you
  var to_page = data.toPage[0].id;

  // skip showing #myPage if condition is true
  if (to_page == "myPage" && condition == 0) {

    // true! go to p3 instead
    $.mobile.changePage("#p3", {
        transition: "flip"
    });

    /* prevent updating URL history and allow new transition if you want.
       Without this, #myPage will be pushed into $.mobile.urlHistory
       and any new transition (animation) will be neglected */        
    e.preventDefault();
  }
  /* in the demo, if you change condition value, #p2 will be shown
     when condition returns "false". #myPage will be shown normally */
});

注: pagebeforechange 2 回発火しますが、これは正常です。パニックにならないでください ;)

于 2013-10-22T15:51:47.880 に答える
0

これを行う最善の方法は、デフォルトでページを非表示にし、条件を満たした場合にのみ表示することだと思います。だから、それはこのようになります

CSS:

#myPage {
  display: none;
}

JS:

$(document).on("pagebeforeshow", "#myPage", function(){ 
  if(condition){
    $.mobile.changePage("example.jsp");
  } else {
    $('#myPage').show()
  }
});
于 2013-10-22T14:08:03.650 に答える