简体   繁体   English

jQuery Mobile导航不起作用

[英]jQuery Mobile navigation not working

I'm currently working on a web project with jQuery mobile but navigation just won't work. 我目前正在使用jQuery mobile进行网络项目,但导航无法正常工作。

When i hit a page from the menu (eg. "customers") the url displays correctly ( www.aaa.bb/#customers ) and the page is loaded without any problems. 当我从菜单(例如“客户”)中点击页面时,URL会正确显示( www.aaa.bb/#customers ),并且页面加载没有任何问题。 When i hit another page the url is also correct and the page loads but when i then try to go back using the browser's back button the page doesn't change even though the url changes correctly.. 当我点击另一个页面时,URL也是正确的,并且页面已加载,但是当我然后尝试使用浏览器的后退按钮返回时,即使URL正确更改,页面也不会更改。

I have seperate files for my different pages. 我的不同页面有单独的文件。 Could this cause the problem? 这会引起问题吗?

If so, why does the url change correctly then? 如果是这样,为什么URL会正确更改呢?

Thank you! 谢谢!

Edit: Ajax is enabled on my page 编辑:我的页面上启用了Ajax

If you are using AJAX to navigate throughout your site it is necessary to update the history yourself using history.pushState , since your HTTP (ajax) call will otherwise not be logged and stored in the history. 如果您使用AJAX在整个站点中导航,则必须使用history.pushState自己更新历史记录,因为否则HTTP(ajax)调用将不会被记录并存储在历史记录中。

There are various ways you can manipulate the history, or change how you AJAX calls get fired, here is a solid doc on how to manipulate browser history, follow this up by creating a function that checks onpopstate event that is fired and update your page. 您可以通过多种方式来处理历史记录,或更改触发AJAX调用的方式, 是有关如何处理浏览器历史记录的可靠文档,可通过创建一个功能来检查onpopstate事件并更新页面来进行后续操作。

After hours of reading docs and testing different attempts I wrote this solution: 经过数小时的文档阅读和测试尝试,我编写了以下解决方案:

$(window).on("navigate", function(){
    var file="/";

    if(typeof history.state.pageUrl != 'undefined') {
        file += history.state.pageUrl + ".php";
    }

    else{
        file += "index.php";
    }


    $(":mobile-pagecontainer").pagecontainer( "change", root + file, {"reverse": true} );
});

This solution only works for my specific problem and may not work for others. 此解决方案仅适用于我的特定问题,可能不适用于其他人。

Known limitations: 已知限制:

  • all your jQuery Mobile pages have to be in the root directory of you website 您所有的jQuery Mobile页面都必须位于网站的根目录中
  • the filenames of your pages have to be equal to their data-url attribute 页面的文件名必须等于其data-url属性
  • all your pages have to have the same file extension (.php) 您的所有页面都必须具有相同的文件扩展名(.php)

Dialogs will probably cause trouble too. 对话框也可能会引起麻烦。 I am also facing a strange issue when navigating to my index.php file. 导航到index.php文件时,我也遇到一个奇怪的问题。

I will improve my solution over time and post it again. 随着时间的推移,我将改善自己的解决方案,然后再次发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM