a标签target属性仔细审题

23次阅读

共计 1404 个字符,预计需要花费 4 分钟才能阅读完成。

a 标签 target 属性仔细审题
我们来看 msdn 对于 a 标签 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, this behaves the same way as _self.

翻译一下,就是该属性可以指定在什么地方展示我们的链接页面。该属性值可以是浏览器上下文比如说 tab window 或者 iframe 的名字,或者关键词. 下面有几个关键字具有特殊含义

_self: 在本上下文下直接加载链接, a 标签的默认行为
_blank: 在一个新的浏览器上下文中打开链接,基本上是个新 tab
_parent: 该上下文的父上下文中加载链接,如果该上下文不是 iframe,行为跟_self 一致
_top: 该上下文的顶部上下文中加载链接,如果该上下文不是 iframe,行为跟_self 一致

白话描述一下,
target 的值你可以指定 iframename 或者 windowname, 一个随意的值,或者那四个特殊关键字
指定 name 将在 iframe 中加载,示例:
<a href=”https://www.baidu.com” target=”iframename”>iframename</a>
<iframe name=”iframename”></iframe>
指定 windowname
<a href=”https://www.baidu.com” target=”windowname”>windowname</a>
<script>
window.open(”, ‘windowname’, ‘_blank’);
</script>
指定随意一个关键字操作类似指定 windowname 新开一个 tab 页,并在里面直接加载新链接
_self 的情况下,就是在当前 tab 直接打开
_blank 是无论如何新开一个 tab
_parent 是有父框架,比如 iframe 上层, 直接刷新 iframe 宿主页面
_top 是当有祖先框架的时候,情况就是 tab 页里有 iframe 套 iframe 的情况,子孙 iframe 的 a 标签设置了_top, 祖先 tab 页被加载新 url

正文完
 0