目录 搜索
Attributes accesskey (attribute) class (attribute) contenteditable (attribute) contextmenu (attribute) data-* (attribute) dir (attribute) draggable (attribute) dropzone (attribute) Global attributes hidden (attribute) id (attribute) itemid (attribute) itemprop (attribute) itemref (attribute) itemscope (attribute) itemtype (attribute) lang (attribute) slot (attribute) spellcheck (attribute) style (attribute) tabindex (attribute) title (attribute) translate (attribute) Elements a abbr address area article aside audio b base bdi bdo blockquote body br button canvas caption cite code col colgroup data datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 head header hr html i iframe img input input type="button" input type="checkbox" input type="color" input type="date" input type="datetime"-local input type="email" input type="file" input type="hidden" input type="image" input type="month" input type="number" input type="password" input type="radio" input type="range" input type="reset" input type="search" input type="submit" input type="tel" input type="text" input type="time" input type="url" input type="week" ins kbd label legend li link main map mark menu menuitem meta meter nav noscript object ol optgroup option output p param picture pre progress q rp rt rtc ruby s samp script section select slot small source span strong style sub summary sup table tbody td template textarea tfoot th thead time title tr track u ul var video wbr Miscellaneous Attributes Block-level elements CORS enabled image CORS settings attributes Element Inline elements Kinds of HTML content Link types Microdata Optimizing your pages for speculative parsing Preloading content Reference Supported media formats Using the application cache Obsolete acronym applet basefont big blink center command content dir element font frame frameset hgroup image input type="datetime" isindex keygen listing marquee nextid noframes plaintext strike tt xmp
文字

HTML <a> 元素  (或锚元素) 可以创建一个到其他网页、文件、同一页面内的位置、电子邮件地址或任何其他URL的超链接。

内容分类

流内容,表述内容,互动的内容,可触的内容。

允许的内容 

Transparent, containing either flow content (excluding interactive content) or phrasing content.

标签省略

不允许,开始标签和结束标签都不能省略。

允许的父元素

Any element that accepts phrasing content, or any element that accepts flow content, but always excluding <a> elements (according to the logical principle of symmetry, if <a> tag, as a parent, can not have interactive content, then the same <a> content can not have <a> tag as its parent).

Permitted ARIA roles

button, checkbox, menuitem, menuitemcheckbox, menuitemradio, option, radio, switch, tab, treeitem

DOM接口

HTMLAnchorElement

属性

该元素支持全局属性。

downloadHTML5此属性指示浏览器下载URL而不是导航到URL,因此将提示用户将其保存为本地文件。如果属性有一个值,它将在保存提示中用作预先填写的文件名 (用户仍然可以根据需要更改文件名)。对允许的值没有限制,但是/和\被转换为下划线。大多数文件系统限制文件名中的一些标点符号,浏览器会相应地调整建议的名称。

注意:

  • 此属性仅适用于同源 URLs。

  • 可以使用 blob: URLs 和 data: URLs 以方便用户下载 JavaScript 方式生成的内容(例如使用在线绘图的Web应用创建的照片)。

  • 如果HTTP头的Content-Disposition:存在,并且赋予了一个和这个属性不同的文件名,HTTP头优先于此属性。

  • 如果这个属性存在 Content-Disposition 被设置为 inline,火狐优先 Content-Disposition,像之前文件名的情况下,而Chrome则优先 download 属性。

href这是一个必需属性为锚定义一个超文本链接来源。这表示链接目标的URL或URL片段。URL片段是由一个hash符号(#),它指定一个内部目标在当前文档中的位置(ID)开头的名字。URL不限于网页(HTTP)的文件。URL可能使用浏览器所支持的任何协议。例如,文件,FTP,大多数用户代理mailto工作。

注意:可以使用href="#top"或者href="#"链接返回到页面顶部。这种行为在HTML5上被指定。

hreflang该属性用于指定链接文档的人类语言。其仅提供建议,并没有内置的功能。hreflang 允许的值由HTML5 BCP47 所决定。

referrer一个指示在获取资源时应该使用哪个提交者 (referer) 的字符串:

  • "no-referrer" 表示 Referer: 头将不会被发送。

  • "no-referrer-when-downgrade" 表示当从使用HTTPS的页面导航到不使用 TLS(HTTPS)的来源 时不会发送 Referer: 头。如果没有指定策略,这将是用户代理的默认行为。

  • "origin" 表示 referrer将会是页面的来源,大致为这样的组合:主机和端口(不包含具体的路径和参数的信息)。

  • "origin-when-cross-origin" 表示导航到其它来源将会限制为这种组合:主机 + 端口,而导航到相同的来源将会包含 referrer的路径。

  • "unsafe-url" 表示 referrer将会包含来源和路径(但是不会是包含密码或用户名的片段)。这种情况是不安全的,因为它可能会将TLS保护的资源的来源和路径泄露给不安全的来源。

rel该属性指定了目标对象到链接对象的关系。该值是空格分隔的列表类型值。

target该属性指定在何处显示链接的资源。 取值为标签(tab),窗口(window),或框架(iframe)等浏览上下文的名称或其他关键词。以下关键字具有特殊的意义:

  • _self: 当前页面加载,即当前的响应到同一HTML 4 frame(或HTML5浏览上下文)。此值是默认的,如果没有指定属性的话。

  • _blank: 新窗口打开,即到一个新的未命名的HTML4窗口或HTML5浏览器上下文

  • _parent: 加载响应到当前框架的HTML4父框架或当前的HTML5浏览上下文的父浏览上下文。如果没有parent框架或者浏览上下文,此选项的行为方式相同_self。

  • _top: IHTML4中:加载的响应成完整的,原来的窗口,取消所有其它frame。 HTML5中:加载响应进入顶层浏览上下文(即,浏览上下文,它是当前的一个的祖先,并且没有parent)。如果没有parent框架或者浏览上下文,此选项的行为方式相同_self

注意:使用target时,考虑添加 rel="noopener norefferrer" 以防止针对 window.opener API 的恶意行为。

type该属性指定在一个MIME type链接目标的形式的媒体类型。其仅提供建议,并没有内置的功能。

过时的

charset已废弃HTML5此属性定义链接资源的字符编码。该值是一个空格- 和/或逗号分隔的RFC 2045中定义的字符集列表。默认值是ISO-8859-1。

Usage note: This attribute is obsolete in HTML5 and should not be used by authors. To achieve its effect, use the HTTP Content-Type: header on the linked URL.

coordsHTML 4only,已废弃HTML5对于使用对象的形状,这个属性使用逗号分隔的数字列表来定义对象在页面上的坐标。nameHTML 4only,已废弃HTML5该属性在页面中定义锚点的目标位置时是必须的。

name的值类似于ID核心属性值,该属性值在文档中是唯一的且由数字字母标示符所组成的。在 HTML 4.01 规范中,idname都可以使用 <a> 元素,只要他们有相同的值。

使用说明:该属性在 HTML5中是过时的, 使用全局属性id来代替。

revHTML 4only,已废弃HTML5该属性用于指定当前文档与被链接文档的关系。用于 <a> 标签的可选属性 rel 和 rev 分别表示源文档与目标文档之间正式的关系和方向。rel 属性指定从源文档到目标文档的关系,而 rev 属性则指定从目标文档到源文档的关系。这两种属性可以在 <a> 标签中同时使用。对于网页编者来说,这个属性很有用,可以通过它来查看外链的来源。

shapeHTML 4only,已废弃HTML5此属性用于定义一个可选的超链接相关的一个数字来创建图像映射区域,该属性的值是圆,默认情况下,多边形,矩形。 所述的coords 属性的格式取决于形状的值。对于圆,该值的x,y,r其中x和y为圆心像素坐标,r是像素值半径。对于矩形,该coords 属性应该是x,y,w,h。x,y值定义的矩形的左上角的位置,而w和h分别定义宽度和高度。多边形形状的值需要x1,y1,x2,y2,......值来定义coords。每对x,y定义多边形的一个点,连续点由直线接合和,最后一个点连接到第一个点。默认为形状的值需要将整个封闭区域,典型的图像,可以使用。

说明:建议使用usemap 属性在<img>元素和相关的<map>元素中来定义热点而不是用形状属性。

示例

链接到外部地址

<!-- anchor linking to external file --><a href="https://www.mozilla.com/">External Link</a>

链接到本页的某个部分

<!-- links to element on this page with id="attr-href" --><a href="#attr-href">Description of Same-Page Links</a>

创建一个可点击的图片

这个小例子使用图像链接到MDN主页。主页将在新的浏览环境,这是一个新的页面或新标签中打开研究。

<a href="https://developer.mozilla.org/en-US/" target="_blank">  <img src="https://mdn.mozillademos.org/files/6851/mdn_logo.png"
       alt="MDN logo" /></a>

结果

创建一个email链接

这是常见的创建按钮或链接,将用户的电子邮件程序打开,让他们发送新邮件。这是通过使用一个mailto链接完成的。这里有一个简单的例子:

<a href="mailto:nowhere@mozilla.org">Send email to nowhere</a>

生成这样的链接: 

有关mailto URL方案的更多细节,比如如何包含主题,正文,或其他预定内容,参考 Email links orRFC 6068.

创建电话链接

提供电话链接有助于用户查看连接到手机的网络文档和笔记本电脑。

<a href="tel:+491570156">+49 157 0156</a>

更详细的语法请查看RFC 2806andRFC 2396。

使用 download 属性保存画布为PNG格式

如果你想允许用户下载一个HTML画布,你可以创建一个下载属性和画布数据作为文件URL链接图像:

var link = document.createElement('a');link.textContent = 'download image';link.addEventListener('click', function(ev) {
    link.href = canvas.toDataURL();
    link.download = "mypainting.png";}, false);document.body.appendChild(link);

可以查看这个例子 http://jsfiddle.net/codepo8/V6ufG/2/

备注

html 3.2 只定义 name, href, rel, rev, 和 title 属性。

无障碍建议

锚点标签常常通过将 href 属性设置为 "#" 或 "javascript:void(0)" 来创造一个能阻止页面刷新的伪按钮的方式被滥用。 这些属性值会在拖动 / 复制链接时导致意外行为,在新窗口 / 新标签打开链接,加入书签以及 JavaScript 仍在下载时会出现错误或被禁用。这也会向辅助技术(如屏幕阅读器)传达不正确的语义。在这些情况下,推荐使用 <button> 来代替。通常情况下,您应该只将锚点用于正常的 URL 导航。 

Clicking 及 focus

点击一个 <a> 标签是否会出现 focus 取决于浏览器和操作系统。

Desktop Browsers

Windows 8.1

OS X 10.9

Firefox 30.0

Yes

Yes

Chrome ≥39 (Chromium bug 388666)

Yes

Yes

Safari 7.0.5

N/A

Only when it has a tabindex

Internet Explorer 11

Yes

N/A

Presto (Opera 12)

Yes

Yes

Mobile Browsers

iOS 7.1.2

Android 4.4.4

Safari Mobile

Only when it has a tabindex

N/A

Chrome 35

???

Only when it has a tabindex

规范

规范

状态

评论

引用者策略该规范中“引用者属性”的定义。

编辑草稿

添加了引荐来源属性。

HTML生活标准该规范中'<'的定义。

生活水平


HTML5该规范中<a>的定义。

建议


HTML 4.01规范该规范中'<'的定义。

建议


浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

1.0

(Yes)

1.0 (1.7 or earlier) 1

1.0 4

(All)

1.0

href="#top"

(Yes)

(Yes)

10.0 (10.0)

(Yes)

(Yes)

(Yes)

download

14

13 3

20.0 (20.0)

No support

15

10.1

ping

(Yes)

No support

No support 2

No support

(Yes)

No support

referrerpolicy

46.0 2

No support

50 (50)

No support

No support

No support

Feature

Android

Android Webview

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Chrome for Android

Basic support

(All)

(All)

(Yes)

1.0 (1.0) 1

(All)

(All)

1.0

1.0

href="#top"

(Yes)

?

(Yes)

10.0 (10.0)

(Yes)

(Yes)

(Yes)

?

download

(Yes)

?

(Yes)

20.0 (20.0)

No support

?

No support

?

ping

?

?

No support

No support

?

?

?

?

referrerpolicy

No support

46.0 3

No support

50.0 (50.0)

No support

No support

No support

46.0 3

上一篇: 下一篇: