如何让 HTML 在另一个窗口或选项卡中打开超链接?

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

How to make HTML open a hyperlink in another window or tab?

htmlwindowhyperlink

提问by brilliant

This is a line for a hyperlink in HTML:

这是 HTML 中超链接的一行:

<a href="http://www.starfall.com/">Starfall</a>

Thus, if I click on "Starfall" my browser - I am using FireFox - will take me to that new page and the contents of my window will change. I wonder, how can I do this in HTML so that the new page is opened in a new window instead of changing the previous one? Is there such a way in HTML?

因此,如果我单击“Starfall”,我的浏览器(我使用的是 FireFox)将带我到那个新页面,并且我的窗口内容会发生变化。我想知道,如何在 HTML 中执行此操作,以便在新窗口中打开新页面而不是更改前一个页面?HTML中有这样的方法吗?

And if yes, is there a way to open the requested page in another tab (not another window) of my browser?

如果是,有没有办法在浏览器的另一个选项卡(而不是另一个窗口)中打开请求的页面?

回答by Frank

<a href="http://www.starfall.com/" target="_blank">Starfall</a>

Whether it opens in a tab or another window though is up to how a user has configured her browser.

它是否在选项卡或另一个窗口中打开取决于用户如何配置她的浏览器。

回答by Turnkey

Simplest way is to add a target tag.

最简单的方法是添加目标标签。

<a href="http://www.starfall.com/" target="Starfall">Starfall</a>

Use a different value for the target attribute for each link if you want them to open in different tabs, the same value for the target attribute if you want them to replace the other ones.

如果您希望它们在不同的选项卡中打开,请为每个链接的目标属性使用不同的值,如果您希望它们替换其他选项卡,则为目标属性使用相同的值。

回答by jldupont

use target="_blank"

target="_blank"

<a target='_blank' href="http://www.starfall.com/">Starfall</a>

<a target='_blank' href="http://www.starfall.com/">Starfall</a>

回答by Jason

You should be able to add

你应该能够添加

target="_blank"

like

喜欢

<a href="http://www.starfall.com/" target="_blank">Starfall</a>

回答by hellol11

The targetattribute is your best way of doing this.

target属性是您执行此操作的最佳方式。

<a href="http://www.starfall.com" target="_blank">

will open it in a new tab or window. As for which, it depends on the users settings.

将在新选项卡或窗口中打开它。至于哪个,这取决于用户的设置。

<a href="http://www.starfall.com" target="_self">

is default. It makes the page open in the same tab (or iframe, if that's what you're dealing with).
The next two are only good if you're dealing with an iframe.

是默认的。它使页面在同一选项卡(或 iframe,如果这是您正在处理的)中打开。
仅当您处理 iframe 时,接下来的两个才好。

<a href="http://www.starfall.com" target="_parent">

will open the link in the iframe that the iframe that had the link was in.

将在具有链接的 iframe 所在的 iframe 中打开链接。

<a href="http://www.starfall.com" target="_top">

will open the link in the tab, no matter how many iframes it has to go through.

将在选项卡中打开链接,无论它必须经过多少个 iframe。

回答by The Andyman

the target = _blank is will open in new tab or windows based on browser setting.

target = _blank 将根据浏览器设置在新选项卡或窗口中打开。

To force a new window use javascript onclick all three parts are needed. url, a name, and window width and height size or it will just open in a new tab.

要强制新窗口使用 javascript onclick 需要所有三个部分。url、名称以及窗口宽度和高度大小,否则它将在新选项卡中打开。

<a onclick="window.open('http://www.starfall.com/','name','width=600,height=400')">Starfall</a>

回答by patrick

You can also accomplish this by adding the following to your page's header:

您还可以通过将以下内容添加到页面标题中来完成此操作:

<base target="_blank">

This will make ALL links on your page open in a new tab

这将使您页面上的所有链接在新选项卡中打开

回答by JoeMecPak

Since web is evolving quickly, some things changes with time. For security issues, you might want to use the rel="noopener"attribute in conjuncture with your target="_blank".

由于网络发展迅速,有些事情会随着时间而改变。对于安全问题,您可能希望将该rel="noopener"属性与target="_blank".

Like stated in Google Dev Documentation, the other page can access your window object with the window.opener property. Your external link should looks like this now:

就像Google Dev 文档中所述,另一个页面可以使用window.opener property. 您的外部链接现在应如下所示:

<a href="http://www.starfall.com/" target="_blank" rel="noopener">Starfall</a>

回答by scottmoo

below example with target="_blank"works for Safari and Mozilla

以下示例target="_blank"适用于 Safari 和 Mozilla

<a href="http://www.starfall.com" `target="_blank"`>

Using target="new"worked for Chrome

使用target="new"适用于 Chrome

<a href="http://www.starfall.com" `target="new"`>