--- title: 'HTMLDetailsElement: toggle イベント' slug: Web/API/HTMLDetailsElement/toggle_event tags: - Event - HTMLDetailsElement - Reference - details - events - toggle - イベント translation_of: Web/API/HTMLDetailsElement/toggle_event ---
toggle イベントは、 {{HtmlElement("details")}} 要素の open/closed の状態がトグル切り替えされたときに発生します。
| バブリング | なし |
|---|---|
| キャンセル | 不可 |
| インターフェイス | {{DOMxRef("Event")}} |
| イベントハンドラープロパティ | なし |
| 既定のアクション | {{HtmlElement("details")}} 要素の open の状態をトグル切り替えする。 |
この例は開かれた節をログ出力します。節が閉じられるとログから削除されます。
<aside id="log">
<b>Open chapters:</b>
<div data-id="ch1" hidden>I</div>
<div data-id="ch2" hidden>II</div>
<div data-id="ch3" hidden>III</div>
</aside>
<section id="summaries">
<b>Chapter summaries:</b>
<details id="ch1">
<summary>Chapter I</summary>
Philosophy reproves Boethius for the foolishness of his complaints against Fortune. Her very nature is caprice.
</details>
<details id="ch2">
<summary>Chapter II</summary>
Philosophy in Fortune's name replies to Boethius' reproaches, and proves that the gifts of Fortune are hers to give and to take away.
</details>
<details id="ch3">
<summary>Chapter III</summary>
Boethius falls back upon his present sense of misery. Philosophy reminds him of the brilliancy of his former fortunes.
</details>
</section>
body {
display: flex;
flex-direction: row-reverse;
}
#log {
flex-shrink: 0;
padding-left: 3em;
}
#summaries {
flex-grow: 1;
}
function logItem(e) {
const item = document.querySelector(`[data-id=${e.target.id}]`);
item.toggleAttribute('hidden');
}
const chapters = document.querySelectorAll('details');
chapters.forEach((chapter) => {
chapter.addEventListener('toggle', logItem);
});
{{EmbedLiveSample("Examples", 700, 200)}}
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('HTML WHATWG', 'indices.html#event-toggle', 'toggle event')}} | {{Spec2("HTML WHATWG")}} |
{{Compat("api.HTMLDetailsElement.toggle_event")}}