HTMLElement: attachInternals() 方法
HTMLElement.attachInternals() 方法返回一個 ElementInternals 物件。此方法允許 自定義元素 參與 HTML 表單。ElementInternals 介面提供了用於處理這些元素的實用工具,其方式與處理任何標準 HTML 表單元素相同,並且還向元素公開了 可訪問性物件模型 (Accessibility Object Model)。
語法
js
attachInternals()
引數
無。
返回值
一個 ElementInternals 物件。
異常
NotSupportedErrorDOMException-
如果該元素不是自定義元素,則會丟擲此異常。
NotSupportedErrorDOMException-
如果在定義元素時停用了 "internals" 功能,則會丟擲此異常。
NotSupportedErrorDOMException-
如果在同一個元素上兩次呼叫此方法,則會丟擲此異常。
示例
下面的示例演示瞭如何使用 HTMLElement.attachInternals 建立一個自定義的與表單關聯的元素。然後,將 ElementInternals.form 屬性列印到控制檯,以證明我們有一個 ElementInternals 物件。
js
class CustomCheckbox extends HTMLElement {
static formAssociated = true;
constructor() {
super();
this.internals_ = this.attachInternals();
}
// …
}
window.customElements.define("custom-checkbox", CustomCheckbox);
let element = document.getElementById("custom-checkbox");
console.log(element.internals_.form);
規範
| 規範 |
|---|
| HTML # dom-attachinternals |
瀏覽器相容性
載入中…