【问题标题】:how to set a Jquery Mobile page data-url attribute?如何设置 Jquery Mobile 页面 data-url 属性?
【发布时间】:2012-07-16 19:42:40
【问题描述】:

在从 page1 转到 page2 时尝试手动设置页面上的 data-url 属性时,我遇到了奇怪的效果。

我想将页面 data-url 属性设置为 URL 路径名,如下所示:

 page.attr({ 'data-url' : $.mobile.path.parseUrl( window.location.href ).pathname });

现在我正在 pagebeforeshow 上执行此操作。问题是,如果我不等待至少 400 毫秒,则 data-url 将始终设置为上一页 url。所以我就这样做了,我认为这真的很糟糕......

window.setTimeout(function () {
    page.attr({
        'data-url': $.mobile.path.parseUrl(window.location.href).pathname
    });
}, 400)

问题
这可能是因为我在听 pagebeforeshow 和 pageshow 吗?我如何确保在没有超时的情况下将新页面拉入 DOM 不会获得先前访问过的页面的路径名(这会导致一些混乱的导航......

【问题讨论】:

    标签: jquery events url jquery-mobile


    【解决方案1】:

    问题在于 pushstate 隐藏了实际发生的情况。

    如果您在 Jquery Mobile 中从 www.site.com/folder/page1.html 转到 page2.html,您的 none pushstate Url 如下所示:

     www.site.com/folder/page1.html/#/folder/page2.html
    

    将状态掩码放入

      www.site.com/folder/page2.html
    

    但是如果你使用 parseURl.pathname,路径还是会是

      www.site.com/folder/page1.html
    

    因此,将 data-url 设置为 pathname 将始终将其设置为

       www.site.com/folder/page1.html
    

    因为那是……路径名。 Pushstate 只是没有显示它。我现在正在这样做,它可以在没有超时的情况下正常工作:

       setPage = $.mobile.path.parseUrl( window.location.href );
       setPageUrl = setPage.hash == "" ? setPage.pathname : setPage.hash.replace('#','');
       page.attr({ 'data-url' : setPageUrl });
    

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2011-12-28
      • 2020-04-27
      • 2012-08-15
      • 1970-01-01
      相关资源
      最近更新 更多