如何在 HTML 的新选项卡中打开链接?

2022-01-30 00:00:00 hyperlink html anchor browser-tab

我正在处理一个 HTML 项目,但我不知道如何在没有 JavaScript 的情况下在新选项卡中打开链接.

I'm working on an HTML project, and I can't find out how to open a link in a new tab without JavaScript.

我已经知道 <a href="http://www.WEBSITE_NAME.com"></a> 在同一选项卡中打开链接.任何想法如何使它在一个新的打开?

I already know that <a href="http://www.WEBSITE_NAME.com"></a> opens the link in the same tab. Any ideas how to make it open in a new one?

推荐答案

将链接的target属性设置为_blank:

<a href="#" target="_blank" rel="noopener noreferrer">Link</a>

有关其他示例,请参见此处:http://www.w3schools.com/tags/att_a_target.asp

For other examples, see here: http://www.w3schools.com/tags/att_a_target.asp

我之前建议使用 blank 而不是 _blank,因为如果使用它,它会打开一个新选项卡,然后在再次单击链接时使用相同的选项卡.然而,这仅仅是因为,正如 GolezTrol 所指出的,它指的是框架/窗口的名称 a,当再次按下链接以在同一选项卡中打开它时,将设置和使用该名称.

I previously suggested blank instead of _blank because, if used, it'll open a new tab and then use the same tab if the link is clicked again. However, this is only because, as GolezTrol pointed out, it refers to the name a of a frame/window, which would be set and used when the link is pressed again to open it in the same tab.

rel="noopener noreferrer"是为了防止新打开的标签页恶意修改原来的标签页.有关此漏洞的更多信息,请阅读以下文章:

The rel="noopener noreferrer" is to prevent the newly opened tab from being able to modify the original tab maliciously. For more information about this vulnerability read the following articles:

  • 目标=_blank"漏洞举例
  • 使用 target='_blank 的外部链接'

相关文章