0Pricing
Web Accessibility Academy · 课时

对话框角色与原生 dialog 元素

将图层标记为真正的模态对话框。

对话框角色与原生 dialog 元素 是 CoddyKit 上的免费 Web Accessibility Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web Accessibility Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web Accessibility Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Makes a Dialog a Dialog

A dialog is a layer that interrupts the page to ask for input or show a message. Screen reader users need to be told it is a dialog, not just floating text.

A div Looks Modal but Is Not

A styled div can look like a popup, yet assistive tech sees ordinary content. Without the right role, users never learn a dialog opened.

<div class="modal">Are you sure?</div>

Meet the Native dialog Element

The HTML dialog element is built for this job. It carries dialog semantics for free, so screen readers announce it correctly.

<dialog>Are you sure?</dialog>

Open It as a True Modal

Call showModal() in JavaScript to open dialog as a real modal. This is what makes the page behind it inert and unreachable.

document.querySelector("dialog").showModal();

show() Is Not Modal

Watch out: show() opens the dialog as non-modal, so the page stays interactive behind it. For a blocking dialog, you want showModal().

dialog.show();

The role Behind dialog

Under the hood, the element maps to the dialog role. If you must build one from a div, role="dialog" is the attribute that gives it meaning.

<div role="dialog">Settings</div>

alertdialog for Urgent Choices

When a dialog demands a response, like a delete confirmation, use role="alertdialog". It signals extra urgency to assistive tech.

<div role="alertdialog">Delete this file?</div>

Every Dialog Needs a Name

A dialog must have an accessible name so users know its purpose on open. Point aria-labelledby at the heading inside it.

<dialog aria-labelledby="t"><h2 id="t">Confirm</h2></dialog>

Close It in Code

The native element offers a matching close() method. Calling it dismisses the dialog and lets the page behind become reachable again.

dialog.close();

The Built-In Backdrop

A modal dialog gets a free backdrop you can style with the ::backdrop pseudo-element. It dims the page and signals focus has shifted.

dialog::backdrop { background: rgba(0,0,0,0.5); }

Why Native Saves You Work

The native dialog handles focus, Escape, and inerting the background for you. Reaching for it means fewer bugs than a hand-rolled div.

Quick Check

You want a confirmation popup that blocks the page until the user responds. Which call should you use?

Recap: Use Real Dialogs

You learned a modal needs dialog semantics. Prefer the native dialog with showModal(), give it a name, and reserve alertdialog for urgent choices.

常见问题解答

「对话框角色与原生 dialog 元素」课时是免费的吗?

是的 — 「对话框角色与原生 dialog 元素」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web Accessibility Academy 课程的其余内容,请升级到 CoddyKit PRO。 Web Accessibility Academy 课程共包含 4 节课。

「对话框角色与原生 dialog 元素」这节课中我会学到什么?

将图层标记为真正的模态对话框。 你通过在浏览器中直接运行的动手代码来练习 Web Accessibility Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web Accessibility Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web Accessibility Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「对话框角色与原生 dialog 元素」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web Accessibility Academy 课中编写并运行代码吗?

能。每节 Web Accessibility Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 对话框角色与原生 dialog 元素
  2. 将焦点限制在模态框内
  3. 按 Escape 关闭并恢复焦点
  4. 使用 inert 背景与 aria-modal
← 返回 Web Accessibility Academy