Element:ariaActiveDescendantElement 属性

基准线 2025
新推出

自 ⁨2025 年 4 月⁩起,此功能适用于最新设备和浏览器版本。此功能可能不适用于较旧的设备或浏览器。

Element 接口的 ariaActiveDescendantElement 属性表示当焦点位于 composite 部件、comboboxtextboxgroupapplication 上时的当前活动元素。

有关该属性和特性的用法,请参阅 aria-activedescendant 主题。

HTMLElement 的一个子类,表示活动后代,如果没有活动后代则为 null

描述

该属性是使用 aria-activedescendant 属性的一个灵活替代方案。与 aria-activedescendant 不同,分配给此属性的元素不必具有 id 属性。

该属性会反映元素 aria-activedescendant 属性(当其已定义时),但仅限于匹配有效作用域内元素的引用 id 值。如果设置了该属性,则会清除相应的属性。有关反射元素引用和作用域的更多信息,请参阅 Reflected attributes 指南中的 Reflected element references

示例

获取活动后代

此示例展示了如何使用 ariaActiveDescendantElement 来获取当前的活动后代。

HTML

HTML 定义了一个列表框,用于选择不同类型的街道,它由一个具有 listbox role<div> 元素以及每个选项的嵌套 <div> 项组成。活动后代最初通过 aria-activedescendant 设置为 idavenue 的元素。

html
<div id="streetType" role="listbox" aria-activedescendant="avenue">
  <div>Street</div>
  <div id="avenue">Avenue</div>
  <div>Lane</div>
</div>

JavaScript

下面的代码首先检查 ariaActiveDescendantElement 是否受支持。如果属性受支持,代码将使用 Element.getAttribute() 记录 aria-activedescendant 的值(被引用元素的 id)、属性元素及其文本内容。

js
// Feature test for ariaActiveDescendantElement
if ("ariaActiveDescendantElement" in Element.prototype) {
  log(`getAttribute(): ${streetType.getAttribute("aria-activedescendant")}`);
  log(`ariaActiveDescendantElement: ${streetType.ariaActiveDescendantElement}`);
  log(`text: ${streetType.ariaActiveDescendantElement?.textContent.trim()}`);
} else {
  log("ariaActiveDescendantElement not supported by browser");
}

结果

下面的日志显示了上述代码的输出。从 aria-activedescendant 属性返回值应为 "avenue",关联的元素应为 HTMLDivElement 元素,该元素中的文本应为 Avenue

规范

规范
无障碍富互联网应用程序 (WAI-ARIA)
# dom-ariamixin-ariaactivedescendantelement

浏览器兼容性

另见