jQuery后退按钮. [英] Jquery Back Button.

查看:128
本文介绍了jQuery后退按钮.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将运行先前运行功能的按钮.即我单击按钮A以运行功能A,然后单击按钮B以运行功能B,我想要一个按钮,该按钮允许用户通过单击后退按钮返回功能A.

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.

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

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};

推荐答案

使用哈希超链接和hashchange事件.在jQuery中:

Use hash hyperlinks and the hashchange event. In 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:

<a href="#hash1">function A</a>
<a href="#hash2">function B</a>

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

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.

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

警告:较旧的浏览器,例如IE7 可能不支持hashchange history.go().

Warning: older browsers like IE7 may not support hashchange or history.go().

这篇关于jQuery后退按钮.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆