Javascript 为什么历史对<;a>;点击Firefox?
我不明白为什么在Firefox中,Javascript 为什么历史对<;a>;点击Firefox?,javascript,html,Javascript,Html,我不明白为什么在Firefox中,window.history.back()在按钮上起作用: <button onclick="window.history.back()">Go back</button> 返回 但它不适用于链接: <a onclick="window.history.back()">Go back</a> second.html <!DOCTYPE html> <html> <body&g
window.history.back()
在按钮上起作用:
<button onclick="window.history.back()">Go back</button>
返回
但它不适用于链接:
<a onclick="window.history.back()">Go back</a>
second.html
<!DOCTYPE html>
<html>
<body>
<h1>First page</h1>
<br/>
<a href="second.html">Second</a>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Second page</h1>
<a href="third.html">Third</a>
<br/>
<br/>
<button onclick="window.history.back()">Go Back (button)</button>
<br/>
<a href="javascript: void(0);" onclick="window.history.back()">Go Back (a)</a>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Third page</h1>
<br/>
<button onclick="window.history.back()">Go Back (button)</button>
<br/>
<a href="javascript: void(0);" onclick="window.history.back()">Go Back (a)</a>
</body>
</html>
第二页
返回(按钮)
third.html
<!DOCTYPE html>
<html>
<body>
<h1>First page</h1>
<br/>
<a href="second.html">Second</a>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Second page</h1>
<a href="third.html">Third</a>
<br/>
<br/>
<button onclick="window.history.back()">Go Back (button)</button>
<br/>
<a href="javascript: void(0);" onclick="window.history.back()">Go Back (a)</a>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Third page</h1>
<br/>
<button onclick="window.history.back()">Go Back (button)</button>
<br/>
<a href="javascript: void(0);" onclick="window.history.back()">Go Back (a)</a>
</body>
</html>
第三页
返回(按钮)
场景:
$("#back").click(function(e) {
e.preventDefault();
history.back(1);
})
如果您使用返回(按钮)它可以工作。
的区别是什么?我认为这可能与a
标记的href
属性有关,该属性被解释为导航的下一步(即使您回到历史中)
尝试从a
中删除href
属性
如您所见,它的工作方式如下:
现在只需修复a
的CSS即可尝试
window.history.go(-1)
查看了解更多详细信息。您正面临此问题():
但是,当我导航到后按下后退按钮时发生了什么
不同的页面被锁定在最后两个导航页面之间
评论中有人对这个问题一针见血
- 创建
onclick
属性时,您将附加一个用于单击链接的附加事件处理程序。但是,浏览器仍然会处理一些本机逻辑,因此它仍然会将当前页面添加到历史记录中李>
- 当您将特定javascript传递到
href
属性时,实际上您正在覆盖浏览器的本机逻辑,因此它不会将当前页面添加到历史记录中李>
简单、肮脏的解决方案
HTML:
Javascript:
var backbutton = document.getElementById("backButton");
backbutton.onclick = function(e){
e = e || window.event; // support for IE8 and lower
e.preventDefault(); // stop browser from doing native logic
window.history.back();
}
试试这个:
$("#back").click(function(e) {
e.preventDefault();
history.back(1);
})
或
<a href="javascript:void(0);" onclick="window.history.go(-1); return false;"> Link </a>
<代码> >而不是<代码> OnCase=“Windows .Sist.Butter())< /COD> >为什么不考虑使用<代码> HReF=“JavaScript:Windows .Sist.Butter())< /C>?但是我没有询问旁路。我问它为什么是这样工作的?看看链接,它在Firefox 25.0.1中似乎工作得很好。我刚刚在Firefox 25.0.1上测试了它,但它不工作。你能解释一下它会有什么不同吗?会让它后退一步(就在之前访问的页面上)。你的答案不工作!但我并没有问关于旁路的问题。我问为什么会这样。解决方法是window.history.back()不就是这么做的吗?