# HTML 对话框元素

在 HTML 中，`<dialog>` 元素用于定义对话框——可以是模态的，也可以是非模态的——或其他交互式组件，例如可关闭的警告框、检查器或子窗口。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> 该元素定义于 HTML 生活标准中，其 DOM 接口为 `HTMLDialogElement`，在作者未编写任何 ARIA 属性的情况下，带有隐式的 ARIA 角色 `dialog`。<sup>[2](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)</sup><sup> • </sup><sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 标记模态或非模态对话框、可关闭的警告框、检查器或子窗口<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> |
| 打开模式 | `show()` 以非模态方式打开；`showModal()` 在顶层中以模态方式打开<sup>[3](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)</sup> |
| 模态行为 | 背景变为惰性，焦点被限制，按 `Esc` 关闭，关闭时焦点返回先前聚焦的元素<sup>[3](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)</sup> |
| 样式钩子 | 模态对话框背后的 `::backdrop` 伪元素；`:modal` 和 `:open` 伪类匹配对话框状态<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup><sup> • </sup><sup>[4](https://webkit.org/blog/13009/new-webkit-features-in-safari-15-6/)</sup> |
| 浏览器支持 | Chrome 37、Firefox 98、Safari 15.4；自 2022 年 3 月起在各浏览器中广泛可用，全球使用率约 97.12%<sup>[5](https://caniuse.com/mdn-html_elements_dialog)</sup> |
| 无障碍性 | 隐式 `role="dialog"`；通过 `showModal()` 打开的对话框暴露为 `aria-modal="true"`<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> |
| 限制 | 不得在 `<dialog>` 元素上指定 `tabindex` 属性<sup>[2](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)</sup> |

## 模态与非模态模式

该元素提供两种 [JavaScript](https://www.edgepedia.org/zh/javascript) 方法来打开它，每种方法的行为各不相同。<sup>[6](https://modernhtml.dev/elements/the-dialog-element.html)</sup> 调用 `show()` 显示非模态对话框：没有任何内容变为惰性，焦点不被限制，`Escape` 键默认不执行任何操作。<sup>[3](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)</sup> 调用 `showModal()` 以模态方式显示：对话框进入顶层，位于页面上所有其他内容之上，其背后出现 `::backdrop` 伪元素，而对话框之外的所有内容变为惰性——即无法聚焦或点击，并被排除在无障碍树之外。<sup>[3](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)</sup> 顶层位于所有 z-index 堆叠上下文之上，因此模态对话框渲染在页面上所有其他元素之上，包括位于已变换祖先元素内部的固定定位元素。<sup>[6](https://modernhtml.dev/elements/the-dialog-element.html)</sup>

**关闭。** 通过 `showModal()` 打开的模态对话框默认可以通过按 `Esc` 键关闭；非模态对话框默认不通过 `Esc` 关闭。如果打开了多个模态对话框，按 `Esc` 只关闭最后显示的对话框，使用 `<dialog>` 时浏览器提供此行为。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> 按 `Escape` 会触发 `cancel` 事件，代码可以拦截该事件以阻止关闭，随后对话框关闭，并触发 `close` 事件。<sup>[7](https://webkit.org/blog/12209/introducing-the-dialog-element/?trk=article-ssr-frontend-pulse_little-text-block)</sup> 当模态对话框关闭时，浏览器将焦点恢复到调用 `showModal()` 之前聚焦的元素。<sup>[3](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)</sup>

规范警告不要手动移除 `open` 属性来隐藏对话框。这样做通常能隐藏对话框，但不会触发 `close` 事件，导致 `close()` 和关闭请求无法关闭该对话框，并且如果对话框是通过 `showModal()` 显示的，还会使文档保持阻塞状态。推荐的做法是使用 `requestClose()` 或 `close()` 方法关闭。<sup>[2](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)</sup>

## 表单与返回值

对话框内的表单可以使用特殊的表单方法 `dialog`。当提交带有 `method="dialog"` 的表单时，表单不发送网络请求，对话框关闭，对话框的 `returnValue` 属性变为提交表单的按钮的值，并触发 `close` 事件。<sup>[6](https://modernhtml.dev/elements/the-dialog-element.html)</sup> 默认情况下，`returnValue` 为空字符串，或者为对话框中提交表单的按钮的值（如果存在）。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> W3C 的 WCAG 技术 H102 指出，将关闭按钮嵌套在带有 `method="dialog"` 的表单中，可以让浏览器负责将键盘焦点返回到调用元素。<sup>[8](https://www.w3.org/WAI/WCAG22/Techniques/html/H102)</sup>

## 无障碍性

**焦点放置。** 当 `showModal()` 打开对话框时，焦点设置在第一个嵌套的可聚焦元素上。作者可以通过在预期用户首先与之交互的后代元素上使用 `autofocus` 属性来明确指示初始焦点；如有疑问，特别是在内容动态渲染时，`<dialog>` 元素本身可能是最佳的初始焦点放置位置。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> HTML 标准也给出了同样的建议，称对话框聚焦步骤会选择一个候选元素，但不能替代作者的有意选择。<sup>[2](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)</sup>

**关闭机制。** MDN 建议确保所有用户都有可用的关闭对话框机制，并称显式按钮——例如确认、取消或关闭按钮——是最稳健的方式。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> ARIA 创作实践指南同样强烈建议在 Tab 序列中提供一个可见的关闭对话框的按钮。<sup>[9](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/)</sup>

**语义。** 浏览器暴露 `<dialog>` 元素的方式类似于使用 ARIA `role="dialog"` 属性的自定义对话框。通过 `showModal()` 调用的对话框隐式具有 `aria-modal="true"`，而通过 `show()`、`open` 属性或更改默认 `display` 打开的对话框则暴露为 `aria-modal="false"`。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> 该元素仍然需要一个可访问名称，通常通过 `aria-labelledby` 指向其标题。<sup>[3](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)</sup>

## 与自定义对话框的比较

在 Web 历史的大部分时间里，模态对话框意味着一个 `<div role="dialog">`，配合 JavaScript 焦点陷阱在第一个和最后一个可聚焦元素之间循环 `Tab`，对其他所有内容设置 `aria-hidden`，以及一个 `Escape` 处理程序。通过 `showModal()` 打开的 `<dialog>` 元素原生完成了所有这些工作：顶层放置、背景惰性、焦点限制、`Escape` 关闭以及焦点返回。<sup>[3](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)</sup> 自定义实现必须手动复制这些可用性和无障碍性特性，例如对兄弟元素应用 `inert` 属性或 `aria-hidden`；而使用 `<dialog>` 和 `showModal()` 时，浏览器自行提供惰性背景。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup>

该元素还被定位为旧式 JavaScript 对话框的替代品。Chromium 团队建议不要使用 `alert()`、`confirm()` 和 `prompt()`，因为它们同步的、应用模态的 API 会暂停 JavaScript 引擎，并建议使用 HTML `<dialog>` 元素来获取用户输入。<sup>[10](https://developer.chrome.com/blog/dialogs-policy/)</sup>

## 历史与浏览器支持

该元素于 2012 年 4 月在 WHATWG 邮件列表中提出，作为一组用于对话框、弹出窗口、检查器以及将应用程序部分标记为惰性的功能的一部分，包括 `<dialog>`、`showModal()`、`close(returnValue)`、`inert` 属性和 `<form method=dialog>`。<sup>[11](https://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-April/077688.html)</sup> 同月提交了一个实现该元素的 WebKit 缺陷报告；早期实现后来被移除，该元素于 2022 年初在 Safari Technology Preview 134 和 Safari 15.4 beta 中发布。<sup>[12](https://bugs.webkit.org/show_bug.cgi?id=84635)</sup><sup> • </sup><sup>[7](https://webkit.org/blog/12209/introducing-the-dialog-element/?trk=article-ssr-frontend-pulse_little-text-block)</sup> Safari Technology Preview 134 默认启用了该元素，包括 `::backdrop` 伪元素、顶层支持和 `<form method="dialog">` 支持。<sup>[13](https://webkit.org/blog/12033/release-notes-for-safari-technology-preview-134/)</sup>

支持于 2022 年 3 月覆盖所有主要引擎，Firefox 98 和 Safari 15.4 于当月发布该元素；MDN 将其标记为 Baseline，自 2022 年 3 月起在各浏览器中广泛可用。<sup>[5](https://caniuse.com/mdn-html_elements_dialog)</sup><sup> • </sup><sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> Can I use 报告全球使用率约为 97.12%，[Internet Explorer](https://www.edgepedia.org/zh/internet-explorer) 和旧版 Edge（版本 12 至 18）不支持该元素。<sup>[5](https://caniuse.com/mdn-html_elements_dialog)</sup> `:modal` CSS 伪类定义于 CSS 选择器第 4 级中，用于选择通过 `showModal()` 打开的对话框，该伪类在 Safari 15.6 中添加。<sup>[4](https://webkit.org/blog/13009/new-webkit-features-in-safari-15-6/)</sup>

## 较新的增补

**closedby。** `closedby` 属性指定可以关闭对话框的用户操作类型，区分轻触关闭（当用户在对话框外点击或轻触时关闭，等同于“auto”状态弹出框的行为）与其他方法。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)</sup> Safari Technology Preview 242 添加了对该属性的支持。<sup>[14](https://webkit.org/blog/17934/release-notes-for-safari-technology-preview-242/)</sup> 根据标准，当对话框通过 `showModal()` 显示时，Auto 状态表现为关闭请求状态，否则表现为 None 状态。<sup>[2](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)</sup>

**调用器命令。** 调用器命令 API 允许按钮以声明方式打开对话框，无需点击处理程序，使用 `commandfor="confirm" command="show-modal"`；配合 `<form method="dialog">`，触发器或按钮上均无需任何 JavaScript 即可实现确认模态框。<sup>[6](https://modernhtml.dev/elements/the-dialog-element.html)</sup> HTML 标准为 `<dialog>` 定义了有效命令：Close、Request Close 和 Show Modal。<sup>[2](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)</sup>

与所有其他 [HTML 元素](https://www.edgepedia.org/zh/html-element)一样，`dialog` 元素不得用于不同类型的控件：规范指出上下文菜单、工具提示和弹出列表框不是对话框，不应滥用该元素来构建此类模式。<sup>[2](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)</sup>

## References

1. [<dialog> HTML dialog element, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog)
2. [HTML Living Standard, The dialog element, WHATWG](https://html.spec.whatwg.org/multipage/interactive-elements.html?elementdef-dialog=)
3. [Native dialog Versus Custom Focus Traps, Accessible Data Interfaces](https://www.accessible-data-interfaces.com/core-aria-keyboard-navigation-for-data-uis/keyboard-focus-trapping-navigation/native-dialog-versus-custom-focus-traps/)
4. [New WebKit Features in Safari 15.6, WebKit](https://webkit.org/blog/13009/new-webkit-features-in-safari-15-6/)
5. [HTML element: dialog, Can I use](https://caniuse.com/mdn-html_elements_dialog)
6. [Modern HTML: The Dialog Element](https://modernhtml.dev/elements/the-dialog-element.html)
7. [Introducing the Dialog Element, WebKit](https://webkit.org/blog/12209/introducing-the-dialog-element/?trk=article-ssr-frontend-pulse_little-text-block)
8. [H102: Creating modal dialogs with the HTML dialog element, W3C Web Accessibility Initiative](https://www.w3.org/WAI/WCAG22/Techniques/html/H102)
9. [Dialog (Modal) Pattern, ARIA Authoring Practices Guide, W3C](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/)
10. [Chromium policy on JavaScript dialogs, Chrome for Developers](https://developer.chrome.com/blog/dialogs-policy/)
11. [[whatwg] Dialogs and prompts, WHATWG mailing list, April 2012](https://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-April/077688.html)
12. [Bug 84635: Implement the dialog element, WebKit Bugzilla](https://bugs.webkit.org/show_bug.cgi?id=84635)
13. [Release Notes for Safari Technology Preview 134, WebKit](https://webkit.org/blog/12033/release-notes-for-safari-technology-preview-134/)
14. [Release Notes for Safari Technology Preview 242, WebKit](https://webkit.org/blog/17934/release-notes-for-safari-technology-preview-242/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/html-dialog-element

English original: https://www.edgepedia.org/html-dialog-element
