大网数据广告活动
Web 标准与前端开发

ARIA: link role

· 52CE 我爱测

A `link` widget provides an interactive reference to a resource. The target resource can be either external or local; i.e., either outside or within the current page or application.

A link widget provides an interactive reference to a resource. The target resource can be either external or local; i.e., either outside or within the current page or application.

[!NOTE] Where possible, it is recommended that you use a native a element rather than the link role, as native elements are more widely supported by user agents and assistive technology. Native a elements also support keyboard and focus requirements by default, without need for additional customization.

Description

The link role is used to identify an element that creates a hyperlink to a resource that is in the application or external.

When not using semantic HTML for its intended purpose, interactive features must be re-implemented. For example, when role="link" is added to an element, the <kbd>tab</kbd> key should enable giving focus to the link and the <kbd>enter</kbd> key should execute the link when focused.

Use the tabindex attribute with a value of 0 to ensure the link is in the correct tab focus order.

[!WARNING] Applying the link role to an element will not cause browsers to enhance the element with standard link appearance or behaviors, such as underlining, focus rings, navigation to the link target, or context menu actions. That's the developer's responsibility.

Examples

To recreate an accessible link using the link role on an element that is not an a, you need to ensure the link receives focus in the correct tab order, that the element looks like a link, and that the "link" behaves like a link.

<span data-href="https://mozilla.org" tabindex="0" role="link">
  Fake accessible link created using a span
</span>

CSS

span[role="link"] {
  color: blue;
  text-decoration: underline;
  cursor: pointer;
}
span[role="link"]:hover,
span[role="link"]:active,
span[role="link"]:focus {
  color: purple;
}

span[role="link"]:focus {
  background-color: palegoldenrod;
  outline: 1px dotted;
}

JavaScript

const fakeLinks = document.querySelectorAll('[role="link"]');

for (const link of fakeLinks) {
  link.addEventListener("click", navigateLink);
  link.addEventListener("keydown", navigateLink);
}

// handles click and keydown events on the link
function navigateLink(e) {
  if (e.type === "click" || e.key === "Enter") {
    const ref = e.target ?? e.srcElement;
    if (ref) {
      window.open(ref.getAttribute("data-href"), "_blank");
    }
  }
}

If the element with role="link" receives an <kbd>Enter</kbd> key event, this executes the link, going to the linked page or moving focus to the in page target.

Optionally, <kbd>Shift</kbd> + <kbd>F10</kbd> opens a context menu for the link.

Best practices

The various widget roles are used to define common interactive patterns. Similar to the document-structure roles, some of these roles, including the link role, duplicate the semantics of native HTML elements that are well supported, and should not be used.

Avoid using link, which we've included for completeness. The a semantic equivalent with accessible interactivity is available and supported.

Prefer HTML

Using the a instead.

[!NOTE] There is no need to include role="link" on an HTML link as the <a>, by default, has that role already.

Specifications

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。