jquery mobile根据调用页面阻止页面更改

我试图阻止jquery移动中的页面更改取决于用户当前所在的页面,但我不知道data.options对象中的内容。 所以基本上我需要说用户是否要使用index.html并且调用页面是example.html然后防止默认。

$(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(); } }); 

您实际上想要使用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/