DOM
Element 接口的 ariaActiveDescendantElement 属性表示当焦点位于 composite 微件、combobox、textbox、group 或 application 上时当前活动的元素。
aria-activedescendant 页面提供了关于如何使用该属性和对应特性的更多信息。
值
表示活动后代元素的 HTMLElement 子类;如果没有活动后代元素,则为 null。
描述
该属性是使用 aria-activedescendant 属性的灵活替代方案。与 aria-activedescendant 不同,赋给此属性的元素不必具有 id 属性。
定义了元素的 aria-activedescendant 属性时,该属性会反映它,但仅限引用的 id 值与有效作用域内元素匹配的情况。设置此属性会清除对应的属性。有关反射元素引用和作用域的更多信息,请参阅属性反射指南中的反射元素引用。
示例
获取活动后代元素
本示例展示如何使用 ariaActiveDescendantElement 获取当前活动后代元素。
HTML
HTML 定义了一个用于选择不同街道类型的列表框:它由带有 listbox 角色的 div 元素及其嵌套的各选项 <div> 组成。活动后代最初通过 aria-activedescendant 设为 id 为 avenue 的元素。
<div id="streetType" role="listbox" aria-activedescendant="avenue">
<div>街道</div>
<div id="avenue">大道</div>
<div>小巷</div>
</div>
<pre id="log"></pre>
#log {
height: 70px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
JavaScript
以下代码首先检查是否支持 ariaActiveDescendantElement。若支持,则使用 Element.getAttribute() 记录 aria-activedescendant 的值(被引用元素的 id)、属性元素及该元素的文本内容。
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = `${logElement.innerText}${text}\n`;
logElement.scrollTop = logElement.scrollHeight;
}
// 检测 ariaActiveDescendantElement 功能
if ("ariaActiveDescendantElement" in Element.prototype) {
log(`getAttribute(): ${streetType.getAttribute("aria-activedescendant")}`);
log(`ariaActiveDescendantElement: ${streetType.ariaActiveDescendantElement}`);
log(`文本:${streetType.ariaActiveDescendantElement?.textContent.trim()}`);
} else {
log("浏览器不支持 ariaActiveDescendantElement");
}
结果
下面的日志显示以上代码的输出。aria-activedescendant 属性返回的值应为 "avenue",关联元素应为 HTMLDivElement 元素,且该元素的文本应为“大道”。
获取活动后代元素
规范
浏览器兼容性
参见
aria-activedescendant属性- ElementInternals.ariaActiveDescendantElement
- 反射元素引用章节,出自属性反射指南
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
