属性
这个元素包含了全局属性。
警告:tabindex 属性不能被使用在
open
指示这个对话框是激活的和能互动的。当没有设置 open 属性时,对话框不应该显示给用户。推荐使用 .show() 或 .showModal() 方法来渲染对话框,而不是使用 open 属性。
无障碍考虑
在实现一个对话框时,考虑最合适的地方来设置用户焦点是很重要的。通过使用 autofocus 属性明确指出初始焦点的位置,将有助于确保初始焦点被设置到被认为是任何特定对话框的最佳初始焦点位置的元素。由于并不总是知道初始焦点可以设置在对话框中的什么地方,特别是对于对话框的内容在被调用时动态呈现的情况,如果有必要,作者可以决定聚焦于
确保给予用户关闭对话框的机制。最有力的方法是给用户提供一个明确的按钮,如确认、取消或关闭按钮。此外,对于那些使用键盘设备的人来说,Escape 键通常也会用来关闭模态对话框。默认情况下,一个由 showModal() 方法调用的
浏览器使用 ARIA role="dialog" 属性对
确保你的对话框实现不会破坏预期的默认行为,并遵循正确的标签建议。
使用说明
高级示例
当单击“Update details”按钮时,此示例打开一个包含一个表单的弹出对话框。
HTML
html
JavaScript
jsconst updateButton = document.getElementById("updateDetails");
const favDialog = document.getElementById("favDialog");
const outputBox = document.querySelector("output");
const selectEl = favDialog.querySelector("select");
const confirmBtn = favDialog.querySelector("#confirmBtn");
// If a browser doesn't support the dialog, then hide the
// dialog contents by default.
if (typeof favDialog.showModal !== "function") {
favDialog.hidden = true;
/* a fallback script to allow this dialog/form to function
for legacy browsers that do not support
could be provided here.
*/
}
// "Update details" button opens the
updateButton.addEventListener("click", () => {
if (typeof favDialog.showModal === "function") {
favDialog.showModal();
} else {
outputBox.value =
"Sorry, the
}
});
// "Favorite animal" input sets the value of the submit button
selectEl.addEventListener("change", (e) => {
confirmBtn.value = selectEl.value;
});
// "Confirm" button of form triggers "close" on dialog because of [method="dialog"]
favDialog.addEventListener("close", () => {
outputBox.value = `${
favDialog.returnValue
} button clicked - ${new Date().toString()}`;
});
结果
规范
Specification
HTML# the-dialog-element
浏览器兼容性
参见
close 事件
cancel 事件
HTML 表单指南
::backdrop 伪元素
dialog-polyfill
Help improve MDN
Was this page helpful to you?
Yes
No
Learn how to contribute
This page was last modified on 2025年8月6日 by MDN contributors.
View this page on GitHub • Report a problem with this content
GPU显存占用高但利用率低的深度解析 (基于实际案例与技术文档)
C语言数组排序的5种方法(附带实例)