Jquery后退按钮.

Dar*_*agh 3 jquery back

我想创建一个按钮,它将运行以前运行的函数.即我点击按钮A运行功能A然后点击按钮B运行功能B我希望有一个按钮,允许用户通过单击后退按钮返回到功能A.

我以为我可以用一个全局变量来保存以前的语句,但它不会起作用,因为每个运行的函数都会覆盖存储的语句.这里有一些psuodo代码来解释我的意思

var globalvar;
globalvar = functionA;
function B { run globalvar};
Run Code Online (Sandbox Code Playgroud)

Bla*_*ger 12

使用散列超链接和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
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<a href="#hash1">function A</a>
<a href="#hash2">function B</a>
Run Code Online (Sandbox Code Playgroud)

现在,每当用户单击浏览器的"后退"按钮(或触发的HTML按钮history.go(-1))时,它将返回到先前选择的任何哈希并再次触发哈希更改事件.

http://jsfiddle.net/mblase75/hL5FZ/

警告:像IE7这样的旧浏览器可能不支持hashchangehistory.go().