如何使用 window.location 使 HTML a href 超链接打开一个新窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13335954/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:50:03  来源:igfitidea点击:

How can I make a HTML a href hyperlink open a new window using window.location?

htmlhyperlinkwindow.location

提问by TheBlackBenzKid

This is my code:

这是我的代码:

<a href="http://www.google.com" onClick="window.location.href='http://www.yahoo.com';return false;" target="_blank">test</a>

When you click it, it takes you to Yahoo but it does not open a new window?

当您单击它时,它会将您带到雅虎,但它不会打开一个新窗口?

回答by John

<a href="#" onClick="window.open('http://www.yahoo.com', '_blank')">test</a>

Easy as that.

就这么简单。

Or without JS

或者没有 JS

<a href="http://yahoo.com" target="_blank">test</a>