HTMLElement: hidePopover() メソッド

Baseline 2024

Newly available

Since April 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

hidePopover()HTMLElement インターフェイスのメソッドで、ポップオーバー要素(すなわち有効な popover 属性がある要素)を最上位レイヤーから外し、display: none のスタイルを付けます。

hidePopover()popover 属性のついた表示中の要素に対して呼び出された場合、beforetoggle イベントが発行され、続いてポップオーバーが非表示になり、それから toggle イベントが発行されます。要素が既に非表示であれば、エラーが発生します。

構文

js
hidePopover()

引数

なし。

返値

なし (undefined)。

例外

InvalidStateError DOMException

このポップオーバーが既に表示である場合に発生します。

ポップオーバーを非表示にする

次の例では、キーボードの特定のキーを押すことで、ポップオーバーを非表示にする機能を提供します。

HTML

html
<button popovertarget="mypopover">ポップオーバーの表示を切り替え</button>
<div id="mypopover" popover="manual">
  キーボードの <kbd>h</kbd> を押すとポップオーバーが閉じます。
</div>

JavaScript

js
const popover = document.getElementById("mypopover");

document.addEventListener("keydown", (event) => {
  if (event.key === "h") {
    popover.hidePopover();
  }
});

結果

仕様書

Specification
HTML Standard
# dom-hidepopover

ブラウザーの互換性

BCD tables only load in the browser

関連情報