jquery mobile toPage select属性dos'不起作用

jquery mobile toPage select attribute doesn't work

本文关键字:不起作用 dos 属性 mobile toPage select jquery      更新时间:2023-09-26

我有一个jquery移动应用程序,里面有一些页面。第一个页面是登录页面,在用户登录后,我不希望用户再次返回登录页面。

在用户登录之后,将显示名为#映射的CCD_ 1。

要防止这种情况,请使用以下代码:

$(document).on('pagecontainerbeforechange', function (e, ui) {
    var activePage = $(':mobile-pagecontainer').pagecontainer('getActivePage');
    if(activePage.attr('id') === 'map') {
        var test = ui.toPage;
        console.log(test.attr('id');
        // if(test.attr('id') === 'login' && login.status === true) {
        //  console.log('you are alrady logged in');
        //  e.preventDefault();
        //  e.stopPropagation();
        // }
    }
});

当我点击上一页再次进入登录页面时,我得到了这个错误:Uncaught TypeError: test.attr is not a function

什么是错误的,我如何选择测试的attr id

有时ui.toPage是一个字符串,有时它是表示页面的jQuery对象。有时,更改前的页面容器会运行两次,一次与字符串一起运行,另一次与对象一起运行。所以试试这个:

$( document ).on( "pagecontainerbeforechange", function( e, ui ) {
    var from = ui.prevPage ? ui.prevPage.prop("id") : '';
    var to = '';
    if (typeof ui.toPage === 'string') {
        var u = $.mobile.path.parseUrl(ui.toPage);
        to = u.hash || '#' + u.pathname.substring(1);
    } else {
        to = '#' + ui.toPage.prop("id");
    }
  if (from === 'map' && to === '#login') {
      alert('Cannot change to login from map');
      e.preventDefault();
           // remove active class on button
            // otherwise button would remain highlighted
      $.mobile.activePage
                .find('.ui-btn-active')
                .removeClass('ui-btn-active');
   }                
});

演示

此外,.attr("id"(也可以使用,但在jQuery的较新版本中,使用.prop("id(更为正确:http://api.jquery.com/prop/