jQuery Mobile阻止页面更改,具体取决于调用页面 [英] jquery mobile prevent page change depending on calling page

查看:109
本文介绍了jQuery Mobile阻止页面更改,具体取决于调用页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图防止jquery mobile中的页面更改,具体取决于用户当前所处的页面,但我不知道data.options对象中的内容.因此,基本上我需要说一下用户是否要使用index.html,而调用页面是example.html,那么请防止使用默认值.

I'm trying to prevent a page change in jquery mobile depending on what page the user is currently on but I dont know what is in the data.options object. So basically I need to say if the user is going to index.html AND the calling page is example.html then prevent default.

$(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/

I've created a sample here http://jsfiddle.net/kiliman/zMnUM/

这篇关于jQuery Mobile阻止页面更改,具体取决于调用页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆