【问题标题】:How to find the active page of the first pageinit in jQM?jQM中如何找到第一个pageinit的活动页面?
【发布时间】:2012-05-19 22:48:44
【问题描述】:

我需要获取 加载的活动页面元素的ID。我知道我们可以使用$.mobile.activePage 来获取当前页面,并且我们可以使用$.mobile.activePage.attr('id') 获取此元素的ID。但是,在第一次加载应用程序时它是undefined。在我的特定应用程序中,我希望用户能够从任何 URL 输入,并且我需要知道当前加载的页面才能正确初始化我的导航系统

$(document).bind('pageinit', function() { 
    if($.mobile.activePage !== undefined) {
        console.log($.mobile.activePage.attr('id'));
    }
    else {
        console.log('1st page load detected');
    }
});

输出: 1st page load detected 在第一次加载和每个后续页面上的 ID。如何在第一次加载时获取 ID?!?

【问题讨论】:

    标签: jquery-mobile jquery jquery-mobile


    【解决方案1】:

    我认为您的问题是,pageinit 查询活动页面类还为时过早。

    如果您加载第一页,pageinit 将在调用第一页的 changePage 之前触发(第一页也通过 changePage 调用加载)。只有@about JQM 中的第 3639 行,活动页面类被分配给页面。

    到那时,pageinit 早已不复存在。

    如果您想获取第一页的 ID,请使用任何其他可用的 page events

    pagebeforeload = triggered before the load request (inside changepage) is made
    pageload = triggered after the page was loaded
    pagebeforechange = triggered before a changepage is made
    pagechange = triggered after changepage is done
    pagebeforeshow = triggered before the page is shown
    pageshow = triggered when the page is shown
    

    我会使用 pagebeforeshow,或者如果您想调整大量内容,请尝试 pagechange 或 pagebeforechange。

    希望有帮助!

    【讨论】:

    • 是的,频繁是对的。 Pageinit 为时过早。我用 pageshow 对此进行了测试,效果很好。
    • 这是很棒的信息。我想将复杂的滑动导航事件附加到页面......这是最好的钩子?
    • 我会选择 $(document).on('your_swipe_event', 'your_swipe_element', function(){ your_stuff }); - 需要 Jquery 1.7.1,但绑定到文档可确保您的元素得到绑定,即使它们尚未在页面上(类似于 live(),只是应该更快)
    • 好吧,你摇滚!我想我可以在事件中对 $(this) 运行一些过滤器,以确保滑动不在表单滑块或类似的东西上,以防止导航混乱......?反正我会试一试...
    • 如果您需要更多信息,请回复。也可以看看这里:filamentgroup.com/lab/jquery_mobile_pagination_plugin 这让你可以滑动页面或图像或任何开箱即用的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    相关资源
    最近更新 更多