<a>

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

<a> SVG 元素创建指向其他网页、文件、同一页面中的位置、电子邮件地址或任何其他 URL 的超链接。它与 HTML 的 <a> 元素非常相似。

SVG 的 <a> 元素是一个容器,这意味着你可以围绕文本(就像在 HTML 中一样)创建链接,也可以围绕任何形状创建链接。

使用语境

分类容器元素
允许内容可包含任意数量、任意顺序的下列元素
动画元素
描述性元素
形状元素
结构元素
渐变元素
<a>, <clipPath>, <filter>, <foreignObject>, <image>, <marker>, <mask>, <pattern>, <script>, <style>, <switch>, <text>, <view>

属性

download

指示浏览器下载 URL 而不是导航到它,因此用户将被提示将其另存为本地文件。值类型<string>默认值可动画

href

超链接指向的 URL 或 URL 片段。值类型<URL>默认值可动画

hreflang

超链接指向的 URL 或 URL 片段的人类可读语言。值类型<string>默认值可动画

ping 实验性

一个空格分隔的 URL 列表,当跟随超链接时,浏览器会在后台向这些 URL 发送带有 PING 主体的 POST 请求。通常用于跟踪。对于解决相同用例且支持更广泛的功能,请参阅 Navigator.sendBeacon()值类型<list-of-URLs>默认值可动画

referrerpolicy

在获取 URL 时要发送的 Referer值类型no-referrer | no-referrer-when-downgrade | same-origin | origin | strict-origin | origin-when-cross-origin | strict-origin-when-cross-origin | unsafe-url默认值可动画

rel

目标对象与链接对象的关系。值类型<list-of-Link-Types>默认值可动画

目标

链接的 URL 的显示位置。值类型_self | _parent | _top | _blank | <XML-Name>默认值_self可动画

type

链接 URL 的 MIME 类型值类型<string>默认值可动画

xlink:href 已弃用

超链接指向的 URL 或 URL 片段。为兼容旧浏览器可能需要。值类型<URL>默认值可动画

DOM 接口

此元素实现了 SVGAElement 接口。

示例

html
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <!-- A link around a shape -->
  <a href="/docs/Web/SVG/Reference/Element/circle">
    <circle cx="50" cy="40" r="35" />
  </a>

  <!-- A link around a text -->
  <a href="/docs/Web/SVG/Reference/Element/text">
    <text x="50" y="90" text-anchor="middle">&lt;circle&gt;</text>
  </a>
</svg>
css
/* As SVG does not provide a default visual style for links,
   it's considered best practice to add some */

@namespace svg url("http://www.w3.org/2000/svg");
/* Necessary to select only SVG <a> elements, and not also HTML's.
   See warning below */

svg|a:link,
svg|a:visited {
  cursor: pointer;
}

svg|a text,
text svg|a {
  fill: blue; /* Even for text, SVG uses fill over color */
  text-decoration: underline;
}

svg|a:hover,
svg|a:active {
  outline: dotted 1px blue;
}

警告: 由于此元素与 HTML 的 <a> 元素共享其标签名,因此使用 CSS 或 querySelector 选择 a 可能会应用于错误的元素类型。尝试使用 @namespace 规则来区分两者。

规范

规范
Scalable Vector Graphics (SVG) 2
# AElement

浏览器兼容性

另见