ShadowRoot: mode プロパティ
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
mode
は ShadowRoot
の読み取り専用プロパティで、モードを open
と closed
のどちらかで示します。
これはシャドウルートの内部機能を JavaScript からアクセスできるかどうかを定義します。
シャドウルートの mode
が "closed
" であった場合、シャドウルートの内部的な実装は JavaScript からアクセスできず、変更できなくなります。例えば、 <video>
の内部の実装は JavaScript からアクセスも変更もできないのと同様になります。
このプロパティ値を設定するには、Element.attachShadow()
に渡すオブジェクトの options.mode
プロパティを使用するか、シャドウルートを宣言的に作成する場合は、<template>
要素の shadowrootmode
属性を使用します。
値
例
js
// アクセスできない closed のシャドウルートを作成します
let element = document.createElement("div");
element.attachShadow({ mode: "closed" });
element.shadowRoot; // シャドウルートが closed なので null
// アクセスできる open のシャドウルートを作成します
let element2 = document.createElement("div");
element2.attachShadow({ mode: "open" });
console.log(`The shadow is ${element2.shadowRoot.mode}`); // "The shadow is open" と出力
element2.shadowRoot.textContent("Opened shadow"); // シャドウは open なので、外部からアクセスできる
仕様書
Specification |
---|
DOM # dom-shadowroot-mode |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
mode |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.