ElementInternals: form property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

Thuộc tính chỉ đọc form của giao diện ElementInternals trả về HTMLFormElement được liên kết với phần tử này.

Giá trị

Một HTMLFormElement.

Ví dụ

Ví dụ sau đây hiển thị một thành phần hộp kiểm tùy chỉnh bên trong biểu mẫu có ID là myForm. In form.length ra console sẽ cho chúng ta giá trị của HTMLFormElement.length.

html
<form id="myForm">
  <custom-checkbox id="custom-checkbox"></custom-checkbox>
  <custom-label for="custom-checkbox">Join newsletter</custom-label>
</form>
js
class CustomCheckbox extends HTMLElement {
  static formAssociated = true;
  #internals;

  constructor() {
    super();
    this.#internals = this.attachInternals();
  }

  connectedCallback() {
    console.log(this.#internals.form.length);
  }
}

window.customElements.define("custom-checkbox", CustomCheckbox);

Thông số kỹ thuật

Specification
HTML
# dom-elementinternals-form

Tương thích trình duyệt