<dialog>: ダイアログ要素
HTML の <dialog>
要素は、ダイアログボックスや、消すことができるアラート、インスペクター、サブウィンドウ等のような対話的コンポーネントを表します。
コンテンツカテゴリ | フローコンテンツ, 区分化ルート |
---|---|
許可された内容 | フローコンテンツ |
タグの省略 | 不可。開始と終了タグの両方が必要。 |
許可された親要素 | フローコンテンツを受け入れるあらゆる要素 |
暗黙の ARIA ロール | dialog |
許可された ARIA ロール | alertdialog |
DOM インターフェイス | HTMLDialogElement |
属性
使用上の注意
<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>
要素のないブラウザーには、このポリフィルを含めてください。
関連情報
close
イベントcancel
イベント- HTML フォームガイド
::backdrop
擬似要素