javascript 如何使用javascript在同一页面上打开超链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8839029/
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
How to open a hyperlink on the same page with javascript?
提问by user1065673
I am using this to open on a new window:
我正在使用它在新窗口上打开:
<div class="backbutton" OnClick="javascript: window.open('http://www.goesback.com')">Go back</div>
How to amend this if I want onclick to open a hyperlink on the same page?
如果我想 onclick 在同一页面上打开超链接,如何修改?
thanks.
谢谢。
采纳答案by Gabriele Petrioli
You just set the url to the window.location.href
您只需将网址设置为 window.location.href
so
所以
<div class="backbutton" OnClick="window.location.href = 'http://www.goesback.com'">Go back</div>
But you should really just convert it to a regular link
但是您真的应该将其转换为常规链接
<a class="backbutton" href="http://www.goesback.com">Go back</div>
回答by James Hill
Set the window.location.href
property:
设置window.location.href
属性:
<div class="backbutton" OnClick="window.location.href='http://www.goesback.com'">
Go back
</div>
Or just use a plain old anchor
(probably your best option):
或者只是使用一个普通的anchor
(可能是你最好的选择):
<a href="http://www.goesback.com">Go back</a>
Note:the javascript:
prefix is not needed in the onclick
handler.
注:在javascript:
不需要前缀onclick
处理。
回答by swilliams
location.href='http://www.goesback.com'
location.href='http://www.goesback.com'
Though you might want to reconsider using onclick
to trigger that.
尽管您可能想重新考虑使用onclick
来触发它。
回答by Michiel van Oosterhout
<div class="backbutton" OnClick="window.location.href = 'http://www.goesback.com'">Go back</div>
回答by Dan Davies Brackett
Why not just use a simple anchor tag?
为什么不使用简单的锚标签?
<a href="[where you want to go]">Go There</a>
If you need to use JavaScript, the other answers - setting the window.location
property - will do what you want.
如果您需要使用 JavaScript,则其他答案 - 设置window.location
属性 - 将执行您想要的操作。
回答by KoKo
The second parameter of window.open() is a string representing the name of the target window.
window.open() 的第二个参数是一个字符串,表示目标窗口的名称。
Set it to: "_self".
将其设置为:“_self”。
<div class="backbutton" OnClick="javascript: window.open('http://www.goesback.com','_self')">Go back</div>