このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

HTMLDialogElement: show() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2022年3月⁩.

show()HTMLDialogElement インターフェイスのメソッドで、ダイアログをモードレスに表示します。すなわち、ダイアログの外側のコンテンツも操作できる状態にします。

構文

js
show()

引数

なし。

返値

なし (undefined)。

例外

InvalidStateError DOMException

ダイアログが既に開いておりモーダルである場合(つまり、すでに HTMLDialogElement.showModal() で開かれている場合)、例外が発生します。

次の例は単純なボタンで、クリックするとフォームを含むダイアログ (<dialog>) を show() メソッドで開きます。ここから、Cancel ボタンをクリックしてダイアログを閉じたり(HTMLDialogElement.close() メソッドで)、submit ボタンでフォームを送信したりすることができます。

html
<!-- フォームを含む単純なポップアップダイアログボックス -->
<dialog id="favDialog">
  <form method="dialog">
    <section>
      <p>
        <label for="favAnimal">好きな動物:</label>
        <select id="favAnimal" name="favAnimal">
          <option></option>
          <option>ブラインシュリンプ</option>
          <option>レッサーパンダ</option>
          <option>クモザル</option>
        </select>
      </p>
    </section>
    <menu>
      <button id="cancel" type="reset">キャンセル</button>
      <button type="submit">確認</button>
    </menu>
  </form>
</dialog>

<menu>
  <button id="updateDetails">詳細を更新</button>
</menu>
js
const updateButton = document.getElementById("updateDetails");
const cancelButton = document.getElementById("cancel");
const dialog = document.getElementById("favDialog");
dialog.returnValue = "favAnimal";

function openCheck(dialog) {
  if (dialog.open) {
    console.log("Dialog open");
  } else {
    console.log("Dialog closed");
  }
}

// 更新ボタンでモーダルダイアログを開く
updateButton.addEventListener("click", () => {
  dialog.show();
  openCheck(dialog);
});

// フォームのキャンセルボタンでダイアログボックスを閉じる
cancelButton.addEventListener("click", () => {
  dialog.close("animalNotChosen");
  openCheck(dialog);
});

仕様書

Specification
HTML
# dom-dialog-show-dev

ブラウザーの互換性

関連情報

  • このインターフェイスを実装している HTML 要素: <dialog>