2012-03-08 83 views
1

我试图阻止jquery mobile中的页面更改,具体取决于用户当前处于什么页面,但我不知道data.options对象中的内容。所以基本上我需要说,如果用户要去index.html和调用页面是example.html,然后防止默认。jquery mobile根据调用页面阻止页面更改

$(document).bind("pagebeforeload", function (event, data) { 
    var toPage = data.toPage; 
    if (toPage == 'undefined') { 
    return; 
    } else { 
    //need additional condition to see current page in the data.objections object? 
    if (toPage == '/android_asset/www/index.html'); 
    event.preventdefault(); 
    } 
}); 

回答

15

您实际上想要使用pagebeforechange事件。

$(document).bind('pagebeforechange', function(e, data) { 
    var to = data.toPage, 
     from = data.options.fromPage; 

    if (typeof to === 'string') { 
     var u = $.mobile.path.parseUrl(to); 
     to = u.hash || '#' + u.pathname.substring(1); 
     if (from) from = '#' + from.attr('id'); 

     if (from === '#page1' && to === '#page3') { 
      alert('Cannot change to page 3 from page 1'); 
      e.preventDefault(); 

      // remove active class on button 
      // otherwise button would remain highlighted 
      $.mobile.activePage 
       .find('.ui-btn-active') 
       .removeClass('ui-btn-active'); 
     }    
    } 
}); 

我已经在这里创造一个样品http://jsfiddle.net/kiliman/zMnUM/

+0

注意,在JQM 1.3.2(在Chrome上运行桌面),数据参数的.toPage属性不是一个URL,但一个对象,并且名称的页面是在data.toPage ['0']找到id – Wytze 2013-09-19 15:15:50

+0

我认为这是第一次调用的URL和第二次调用的对象。每次导航都会调用此事件两次。 – 2015-11-19 14:48:46