<dialog>: ダイアログ要素

HTML の <dialog> 要素は、ダイアログボックスや、消すことができるアラート、インスペクター、サブウィンドウ等のような対話的コンポーネントを表します。

コンテンツカテゴリ フローコンテンツ, 区分化ルート
許可された内容 フローコンテンツ
タグの省略 不可。開始と終了タグの両方が必要。
許可された親要素 フローコンテンツを受け入れるあらゆる要素
暗黙の ARIA ロール dialog
許可された ARIA ロール alertdialog
DOM インターフェイス HTMLDialogElement

属性

この要素にはグローバル属性があります。

警告: tabindex 属性を <dialog> 要素で使用してはいけません。

open

ダイアログがアクティブで操作で使用できることを示します。 open 属性が設定されていないときは、ダイアログはユーザーに表示するべきではありません

使用上の注意

  • <form> 要素は method="dialog" 属性が指定されていれば、ダイアログを閉じることができます。そのようなフォームが送信されると、 returnValue (en-US) プロパティにフォーム送信するために使用されたボタンの value が設定されて、ダイアログが閉じられます。
  • CSS の ::backdrop 擬似要素を使用して、 <dialog> 要素が HTMLDialogElement.showModal() (en-US) で表示されたときの背後のスタイルを設定することができます。例えば、モーダルダイアログの背後にある操作できないコンテンツを暗くするなどです。

シンプルな例

<dialog open>
  <p>Greetings, one and all!</p>
</dialog>

応用的な例

この例では、 "Update details" ボタンをクリックすると、フォームを含むポップアップダイアログボックスが開きます。

HTML

<!-- Simple pop-up dialog box containing a form -->
<dialog id="favDialog">
  <form method="dialog">
    <p><label>Favorite animal:
      <select>
        <option></option>
        <option>Brine shrimp</option>
        <option>Red panda</option>
        <option>Spider monkey</option>
      </select>
    </label></p>
    <menu>
      <button value="cancel">Cancel</button>
      <button id="confirmBtn" value="default">Confirm</button>
    </menu>
  </form>
</dialog>

<menu>
  <button id="updateDetails">Update details</button>
</menu>

<output aria-live="polite"></output>

JavaScript

var updateButton = document.getElementById('updateDetails');
var favDialog = document.getElementById('favDialog');
var outputBox = document.querySelector('output');
var selectEl = document.querySelector('select');
var confirmBtn = document.getElementById('confirmBtn');

// "Update details" button opens the <dialog> modally
updateButton.addEventListener('click', function onOpen() {
  if (typeof favDialog.showModal === "function") {
    favDialog.showModal();
  } else {
    alert("The <dialog> API is not supported by this browser");
  }
});
// "Favorite animal" input sets the value of the submit button
selectEl.addEventListener('change', function onSelect(e) {
  confirmBtn.value = selectEl.value;
});
// "Confirm" button of form triggers "close" on dialog because of [method="dialog"]
favDialog.addEventListener('close', function onClose() {
  outputBox.value = favDialog.returnValue + " button clicked - " + (new Date()).toString();
});

結果

仕様書

Specification
HTML Standard
# the-dialog-element

ブラウザーの互換性

BCD tables only load in the browser

ポリフィル

<dialog> 要素のないブラウザーには、このポリフィルを含めてください。

dialog-polyfill

関連情報