Element: ariaDetailsElements 屬性

基準線 2025
新推出

自 ⁨2025 年 4 月⁩起,此功能適用於最新裝置和瀏覽器版本。此功能可能不適用於較舊的裝置或瀏覽器。

Element 介面的 ariaDetailsElements 屬性是一個數組,其中包含為應用該屬性的元素提供可訪問詳細資訊的元素。可訪問詳細資訊類似於可訪問描述(參見 ariaDescribedByElements),但提供了更冗長的資訊。

關於此屬性和相關 ARIA 屬性應如何使用的其他資訊,請參見 aria-details 主題。

一個 HTMLElement 的子類的陣列。這些元素的內部文字可以與空格連線起來,以獲得可訪問的詳細資訊。

讀取時,返回的陣列是靜態的且只讀的。寫入時,將複製分配的陣列:之後對陣列的更改不會影響屬性的值。

描述

此屬性是使用 aria-details 屬性設定可訪問詳細資訊的一種靈活替代方案。與 aria-details 不同,分配給此屬性的元素不必具有 id 屬性。

aria-details 屬性定義並匹配有效的範圍內元素時,此屬性將反映該屬性,但僅限於列出的引用 id 值。如果設定了此屬性,則相應的屬性將被清除。有關反映的元素引用和範圍的更多資訊,請參閱Reflected attributes 指南中的 Reflected element references

示例

獲取可訪問的詳細資訊

本示例演示瞭如何使用 ariaDetailsElements 來獲取使用 HTML 中的 aria-details 屬性定義的有關資訊。

HTML

HTML 定義了兩個 <span> 元素,並在 <button>aria-details 屬性中引用了它們的 id。

html
<button aria-details="details1 details2">Button text</button>
…
<span id="details1">Details 1 information about the element.</span>
<span id="details2">Details 2 information about the element.</span>

JavaScript

下面的程式碼首先透過 Element.getAttribute()(一個列出被引用元素 id 值的字串)記錄 aria-details 屬性的值。然後,它檢查 ariaDetailsElements 是否受支援,如果受支援,則記錄其值。最後,它透過遍歷返回的元素並連線它們的內部文字來計算並返回可訪問字串。

js
const buttonElement = document.querySelector("button");
log(`aria-details: ${buttonElement.getAttribute("aria-details")}`);
// Feature test for ariaDetailsElements
if ("ariaDetailsElements" in Element.prototype) {
  // Get ariaDetailsElements
  const buttonElements = buttonElement.ariaDetailsElements;
  log(`ariaDetailsElements: ${buttonElements}`);

  // Accessible details from ariaDetailsElements
  const text = buttonElements.map((e) => e.textContent.trim()).join(" ");
  log(`Accessible details: ${text.trim()}`);
} else {
  log("element.ariaDetailsElements: not supported by browser");
}

結果

下面的日誌顯示了原始元素引用、關聯/返回的元素以及可訪問的詳細資訊。

規範

規範
無障礙富網際網路應用程式 (WAI-ARIA)
# dom-ariamixin-ariadetailselements

瀏覽器相容性

另見