Html HTML5:超链接和新标签

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

HTML5: hyperlink and new tab

htmlurlhyperlinktabs

提问by Ignas Damunskis

I have this hyperlink code:

我有这个超链接代码:

<a href="http://www.youtube.com/user/mamgrow"><img style="position:relative; float:right; height:30px; left:-30px;"  alt="mamgrow facebook" src="images/facebook.png"/></a>

And I want this link to open in a new tab...

我希望此链接在新标签页中打开...

I tried to put this in:

我试着把它放进去:

style="target-new:tab;

But it didn't work. Any ideas?

但它没有用。有任何想法吗?

回答by ztirom

You can easily use the targetattribute like:

您可以轻松使用该target属性,例如:

<a href="http://www.example.com" target="_blank"><img src="your/image" /></a>

If your user has a browser which support tabs, the linked page will opened at a new tab in the active browser window, if set so - mostly it is a default.
Nearly every browser supports this today. See this list on Wikipediafor detailed informations.

如果您的用户有支持选项卡的浏览器,则链接页面将在活动浏览器窗口中的新选项卡中打开(如果设置如此) - 主要是默认设置。
今天几乎每个浏览器都支持这一点。有关详细信息,请参阅维基百科上的此列表。

Here a list of the targetattribute properties in a <a>tag in HTML:

这里是HTML 标签中的target属性属性列表<a>

target="_blank" <!-- opens link in a new window -->
target="_self" <!-- opens link in actual window -->
target="_parent"
target="_top" <!-- both handle frames -->

This part of your code:

这部分代码:

style="target-new:tab;

has no effect, its seems to be not supportedby any modern browser.

没有效果,它似乎不受任何现代浏览器的支持

回答by Nadrendion

Reading the w3schools instructions explains that the syntax for opening a link in a new tab or window (depending on the settings in the web browser) you should add the attribute

阅读 w3schools 说明说明在新选项卡或窗口中打开链接的语法(取决于网络浏览器中的设置)您应该添加属性

target="_blank"

http://www.w3schools.com/html/html_links.asp

http://www.w3schools.com/html/html_links.asp

回答by Scott Hulme

You should be using the following code to open in a new window

您应该使用以下代码在新窗口中打开

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

回答by kevinarpe

Expanding upon @ztirom's answer...

扩展@ztirom 的回答...

Mozilla Developer Networksays:

Mozilla 开发者网络说:

target

Specifies where to display the linked URL. It is a name of, or keyword for, a browsing context: a tab, window, or <iframe>. The following keywords have special meanings:

  • _self: Load the URL into the same browsing context as the current one. This is the default behavior.

  • _blank: Load the URL into a new browsing context. This is usually a tab, but users can configure browsers to use new windows instead.

  • _parent: Load the URL into the parent browsing context of the current one. If there is no parent, this behaves the same way as _self.

  • _top: Load the URL into the top-level browsing context (that is, the "highest" browsing context that is an ancestor of the current one, and has no parent). If there is no parent browsing context, this behaves the same way as _self.

Note: When using target, consider adding rel="noopener noreferrer" to avoid exploitation of the window.opener API.

目标

指定在哪里显示链接的 URL。它是浏览上下文的名称或关键字:选项卡、窗口或 <iframe>。以下关键字具有特殊含义:

  • _self:将 URL 加载到与当前浏览器相同的浏览上下文中。这是默认行为。

  • _blank:将 URL 加载到新的浏览上下文中。这通常是一个选项卡,但用户可以将浏览器配置为使用新窗口。

  • _parent:将 URL 加载到当前浏览器的父浏览上下文中。如果没有父对象,则其行为方式与 _self 相同。

  • _top:将 URL 加载到顶级浏览上下文(即“最高”浏览上下文,它是当前浏览上下文的祖先,并且没有父浏览上下文)。如果没有父浏览上下文,则其行为与 _self 相同。

注意:使用 target 时,请考虑添加 rel="noopener noreferrer" 以避免利用 window.opener API。