我会尽可能清楚地解释这一点。我有一个使用 WebView 的 Android 应用程序,基本上可以将网页加载为我的应用程序。我一切正常,但后退按钮似乎是个问题。我已将此页面全部设置在一个 html 页面上,当单击某些按钮时,它会加载到一个 div 中,以给人一种新页面的感觉,但实际上并没有一个页面。我基本上想要后退按钮(在 android 平板电脑或智能手机上)加载以前加载的 div,但我不知道从哪里开始。这是内容切换 jquery 的样子 -
功能内容切换器(设置){ 变种设置= { contentClass : '.contentToLoad', navigationId : '#sideMenu', servFront:'#clickHomeHome'
};
//Hide all of the content except the first one on the nav
$(settings.contentClass).not(':first').hide();
$(settings.navigationId).find('li:first').addClass('active');
//onClick set the active state,
//hide the content panels and show the correct one
$(settings.navigationId).find('a').click(function(e){
var contentToShow = $(this).attr('href');
contentToShow = $(contentToShow);
//dissable normal link behaviour
e.preventDefault();
//set the proper active class for active state css
$(settings.navigationId).find('li').removeClass('active');
$(this).parent('li').addClass('active');
//hide the old content and show the new
$(settings.contentClass).hide();
contentToShow.show("slow");
});
}
内容切换器(); });
注意:我已经裁剪了一堆只是为了展示它在基本层面上是如何工作的。
有人对从哪里开始有任何建议吗?我只是希望后退按钮功能能够检查存储在某处的已启动的先前 div 名称并加载它。
谢谢!
最佳答案
您可以尝试使用 History API。网上有很多教程,例如这个很好:
http://diveintohtml5.info/history.html
基本上这就是它的工作原理。当用户单击 div 的链接时,显示您将状态推送到历史堆栈。
history.pushState({<object with any information about state>}, pageTitle, newUrl);
这会将状态推送到历史堆栈,这意味着当用户在任何现代浏览器(如 webkit)上按下后退按钮时,它将考虑该状态。当采取后退操作时,它将从历史堆栈中弹出状态。您必须以您认为合适的任何方式聆听和处理此操作:
window.addEventListener("popstate", function(event) {
// event object contains the information from the pushed state
// do whatever needed to load the previous page here
});
History API 要求您以特定方式构建代码以使其正常运行。为此,我建议使用一些现有的框架来为您处理后台事件,例如 Backbone .js。希望这可以帮助。
关于Android web view "back"按钮加载之前加载的div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20619407/