Javascript webkit / Chrome history.back(-1) onclick vs href
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14815838/
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
webkit / Chrome history.back(-1) onclick vs href
提问by Pawel Cioch
Everybody knows that but I'll repeat the problem:
每个人都知道,但我会重复这个问题:
<a href="#" onClick="history.go(-1)">Back</a>
Will not work in WebKit based browsers (Chrome, Safari, Mxthon, etc.)
不适用于基于 WebKit 的浏览器(Chrome、Safari、Mxthon 等)
Another approach (should work) that won't work is:
另一种不起作用的方法(应该有效)是:
<a href="#" onclick="javascript:window.history.back(-1);">Back</a>
You could do this - works! (but that's showing JS in url hint)
你可以这样做 - 有效!(但那是在 url 提示中显示 JS)
<a href="javascript:window.history.back(-1);">Back</a>
To use JS in onclick event you could do this (works, but see comments below):
要在 onclick 事件中使用 JS,您可以这样做(有效,但请参阅下面的评论):
<a onclick="javascript:window.history.back(-1);">Please try again</a>
Missing href will make it work, but then it won't appear as clickable link, you could apply css to make it look like link, apply blue color, underline and cursor hand, but who wants that?
缺少 href 将使其工作,但它不会显示为可点击的链接,您可以应用 css 使其看起来像链接,应用蓝色、下划线和光标手,但谁想要那个?
回答by Pawel Cioch
Finally working solution, tested in IE, FF, Safari, Chrome, Opera, Maxthon:
最终可行的解决方案,在 IE、FF、Safari、Chrome、Opera、Maxthon 中测试:
<a href="#" onclick="window.history.back();return false;">Back</a>
Don't forget semicolon after return false;
返回false后不要忘记分号;
回答by Viishnuu
It works even history.go(-1) also...
它甚至可以运行 history.go(-1) 也......
For Chrome, IE, Mozilla i tested, works fine. use below code:
对于 Chrome、IE、Mozilla,我测试过,工作正常。使用以下代码:
<a href="#" onclick="history.go(-1);return false;" style="text-decoration:underline;">Back</a>
回答by Wim Mostrey
This is the only thing that works on all current browsers:
这是唯一适用于所有当前浏览器的方法:
<script>
function goBack() {
history.go(-1);
}
</script>
<a onclick="goBack()">Back</a>

