ElementInternals: validity 屬性
ElementInternals 介面的只讀屬性 validity 返回一個 ValidityState 物件,該物件表示元素相對於約束驗證的各種有效性狀態。
值
一個 ValidityState 物件。
異常
NotSupportedErrorDOMException-
如果元素沒有將其
formAssociated屬性設定為true,則丟擲此異常。
示例
以下示例顯示了一個自定義的複選框元件,其中 formAssociated 設定為 true,並將 validity.valid 的值記錄到控制檯。
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_.validity.valid);
規範
| 規範 |
|---|
| HTML # dom-elementinternals-validity |
瀏覽器相容性
載入中…