Jquery 后退按钮。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10399188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Back Button.
提问by Darren Murtagh
I want to create a button which will run a previously ran function. ie i click buttonA to run Function A then click button B to run functionB i want to have a button which allows the user to go back to functionA by clicking a back button.
我想创建一个按钮来运行以前运行的功能。即我单击按钮A运行功能A然后单击按钮B运行功能B我想要一个按钮,允许用户通过单击后退按钮返回功能A。
i thought i could a global variable to hold previous statements but it would not work as every function ran would overwrite the stored statements. here is some psuodo code to kind of explain what i mean
我以为我可以用一个全局变量来保存以前的语句,但它不起作用,因为每个运行的函数都会覆盖存储的语句。这是一些伪代码来解释我的意思
var globalvar;
globalvar = functionA;
function B { run globalvar};
回答by Blazemonger
Use hash hyperlinks and the hashchange
event. In jQuery:
使用哈希超链接和hashchange
事件。在 jQuery 中:
$(window).on('hashchange',function() {
var hash = location.hash.substring(1); // strip the leading # symbol
// now run code based on whatever the value of 'hash' is
});
HTML:
HTML:
<a href="#hash1">function A</a>
<a href="#hash2">function B</a>
Now, whenever the user clicks the browser's "back" button (or an HTML button that triggers history.go(-1)
), it will go back to whatever hash was previously selected and trigger the hashchange event again.
现在,每当用户单击浏览器的“后退”按钮(或触发 的 HTML 按钮history.go(-1)
)时,它将返回到先前选择的任何哈希值并再次触发 hashchange 事件。
http://jsfiddle.net/mblase75/hL5FZ/
http://jsfiddle.net/mblase75/hL5FZ/
Warning: older browsers like IE7 may not support hashchangeor history.go()
.
警告:IE7 等较旧的浏览器可能不支持 hashchange或history.go()
.
回答by STEEL
JQuery method, and apply the class back to the link, e.g.:
JQuery 方法,并将类应用回链接,例如:
$(document).ready(function(){
$('a.back').click(function(){
parent.history.back();
return false;
});
});