ElementInternals: shadowRoot property

Thuộc tính chỉ đọc shadowRoot của giao diện ElementInternals trả về ShadowRoot cho phần tử này.

Giá trị

Một ShadowRoot nếu phần tử có shadow root, ngược lại là null.

Ví dụ

Ví dụ sau in giá trị của shadowRoot ra console, ngay sau khi gọi HTMLElement.attachInternals(). Tại thời điểm này, giá trị là null. Sau khi gọi Element.attachShadow(), phần tử có Shadow Root và shadowRoot trả về đối tượng đại diện cho nó.

js
class MyCustomElement extends HTMLElement {
  constructor() {
    super();
    this.internals_ = this.attachInternals();

    console.log(this.internals_.shadowRoot); // null

    this.attachShadow({ mode: "open" });

    console.log(this.internals_.shadowRoot); // a ShadowRoot object
  }
}

Thông số kỹ thuật

Specification
HTML
# dom-elementinternals-shadowroot

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