:对话框元素

:对话框元素

属性

这个元素包含了全局属性。

警告:tabindex 属性不能被使用在

元素上。

open

指示这个对话框是激活的和能互动的。当没有设置 open 属性时,对话框不应该显示给用户。推荐使用 .show() 或 .showModal() 方法来渲染对话框,而不是使用 open 属性。

无障碍考虑

元素的早期实现在某些形式的辅助技术上仍存在可用性问题。为了保证 Safari 15.4 版本以下的无障碍性,推荐使用临时的解决方案(如:a11y-dialog)以获得持续的可用性支持。

在实现一个对话框时,考虑最合适的地方来设置用户焦点是很重要的。通过使用 autofocus 属性明确指出初始焦点的位置,将有助于确保初始焦点被设置到被认为是任何特定对话框的最佳初始焦点位置的元素。由于并不总是知道初始焦点可以设置在对话框中的什么地方,特别是对于对话框的内容在被调用时动态呈现的情况,如果有必要,作者可以决定聚焦于

元素本身,提供最佳的初始焦点位置。

确保给予用户关闭对话框的机制。最有力的方法是给用户提供一个明确的按钮,如确认、取消或关闭按钮。此外,对于那些使用键盘设备的人来说,Escape 键通常也会用来关闭模态对话框。默认情况下,一个由 showModal() 方法调用的

将允许其被 Escape 关闭。非模态对话框默认不会通过 Escape 键来关闭,而且根据非模态对话框所代表的内容,它可能不希望有这种行为。如果打开了多个模态对话框,Escape 只应该关闭最后一个可见对话框。

浏览器使用 ARIA role="dialog" 属性对

元素进行了类似于自定义对话框的暴露。由 showModal() 方法调用的 元素将有一个隐含的 aria-modal="true"。而由 show() 方法调用的 元素,或通过使用 open 属性,或改变 的默认 display 而呈现的,将被暴露为 [aria-modal="false"]。建议使用适当的 showModal() 或 show() 方法来渲染对话框。

确保你的对话框实现不会破坏预期的默认行为,并遵循正确的标签建议。

使用说明

元素可关闭含有属性 method="dialog" 的对话框。当提交表单时,对话框的 returnValue 属性将会等于表单中被使用的提交按钮的 value。

::backdrop CSS 伪元素可用于给使用 HTMLDialogElement.showModal() 显示的

元素背景添加样式,例如在对话框被打开激活时,调暗背景中不可访问的内容。

示例

简单示例

下面的示例会渲染一个非模态对话框。在对话框激活的状态下,点击“OK”按钮将会关闭对话框。提供一种机制来关闭 dialog 元素中的对话框是很重要的。例如,Esc 键在默认情况下不会关闭非模态对话框,同时也不能假设用户可以使用一个物理键盘(例如,使用不带物理键盘的触摸屏设备)。

html

Greetings, one and all!

高级示例

当单击“Update details”按钮时,此示例打开一个包含一个表单的弹出对话框。

HTML

html

>Favorite animal:

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

modally

updateButton.addEventListener("click", () => {

if (typeof favDialog.showModal === "function") {

favDialog.showModal();

} else {

outputBox.value =

"Sorry, the

API is not supported by this browser.";

}

});

// "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种方法(附带实例)