role、button、alert、dialog 和地标
应用 ARIA 角色,向辅助技术传达元素用途
role、button、alert、dialog 和地标 是 CoddyKit 上的免费 HTML Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 HTML Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 HTML Academy 课程共包含 4 节课。
role=button
当非按钮元素必须充当按钮时,可以使用 role="button"(这种情况很少见):
<span
role="button"
tabindex="0"
onclick="handleClick()"
onkeydown="e.key==='Enter'||e.key===' ' ? handleClick() : null"
aria-pressed="false"
>
Toggle Feature
</span>
<!-- Better: use a real <button> element! -->
<!-- Only use role="button" when you cannot change the HTML element -->role=alert
role="alert" 会立即向屏幕阅读器播报内容变化:
<div role="alert" id="error-msg"></div>
<script>
// When injecting an error:
document.getElementById('error-msg').textContent = 'Invalid email address.';
// Screen readers announce this immediately without the user moving focus
// role="alert" implies aria-live="assertive" aria-atomic="true"
</script>role=dialog
role="dialog" 用于标记模态对话框组件:
<div
role="dialog"
aria-modal="true"
aria-labelledby="dialog-title"
aria-describedby="dialog-desc"
id="confirm-dialog"
tabindex="-1"
>
<h2 id="dialog-title">Confirm Deletion</h2>
<p id="dialog-desc">This action cannot be undone.</p>
<button>Cancel</button>
<button>Delete</button>
</div>dialog 元素与 role=dialog
现代 HTML5 提供了原生的 <dialog> 元素:
<dialog id="confirm" aria-labelledby="dialog-title">
<h2 id="dialog-title">Confirm Deletion</h2>
<p>This cannot be undone.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Delete</button>
</form>
</dialog>
<script>
document.getElementById('confirm').showModal();
</script>
<!-- Native dialog handles focus trap and Escape key automatically -->地标角色概览
ARIA 地标角色将页面组织成可导航的区域:
<header role="banner">...</header>
<nav role="navigation">...</nav>
<main role="main">...</main>
<aside role="complementary">...</aside>
<footer role="contentinfo">...</footer>
<form role="form" aria-label="Search">...</form>
<section role="region" aria-label="Stats">...</section>
<!-- Note: prefer semantic HTML elements over ARIA roles
Only add role= when using generic divs/spans -->多个导航地标
页面包含多个 nav 元素时,使用 aria-label 加以区分:
<nav aria-label="Primary navigation">
<ul>...</ul>
</nav>
<nav aria-label="Table of contents">
<ol>...</ol>
</nav>
<nav aria-label="Footer links">
<ul>...</ul>
</nav>
<!-- Screen reader landmark list shows: "Primary navigation, navigation"
"Table of contents, navigation" etc. -->role=status
role="status" 会礼貌地播报非紧急更新:
<!-- Use for: success messages, loading complete, items added -->
<div role="status" aria-live="polite" id="cart-status"></div>
<script>
// When item added to cart:
document.getElementById('cart-status').textContent = '3 items in cart';
// Screen reader announces this when it finishes reading current content
// Less urgent than role="alert"
</script>role=region
role="region" 创建一个具有名称的地标区域:
<section
role="region"
aria-labelledby="stats-heading"
>
<h2 id="stats-heading">Site Statistics</h2>
<dl>...</dl>
</section>
<!-- role="region" only creates a landmark IF it has an accessible name
Without aria-label or aria-labelledby, it is not a landmark -->role=search
role="search" 用于标记搜索功能:
<form role="search" action="/search">
<label for="q">Search</label>
<input type="search" id="q" name="q">
<button type="submit">Go</button>
</form>
<!-- role="search" landmark: screen readers list it as "Search, search"
Users jump directly to the search form -->role=presentation 与 none
role="presentation"(别名:role="none")会移除元素的语义:
<!-- Table used for layout (bad practice, but if inherited): -->
<table role="presentation">
<tr><td>Layout column 1</td><td>Layout column 2</td></tr>
</table>
<!-- role="presentation" removes the table semantics
Screen reader won't announce it as a table
Children's semantics are also affected -->快速检查
role=alert 和 role=status 有什么区别?
回顾:ARIA 角色
常见的 ARIA 角色:
role="button"——非按钮元素充当按钮(添加 tabindex + JS)role="alert"——立即进行强调式播报role="dialog"——模态对话框(优先使用原生 <dialog>)role="status"——礼貌地播报更新role="region"——具有名称的区域地标role="search"——搜索表单地标
常见问题解答
「role、button、alert、dialog 和地标」课时是免费的吗?
是的 — 「role、button、alert、dialog 和地标」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 HTML Academy 课程的其余内容,请升级到 CoddyKit PRO。 HTML Academy 课程共包含 4 节课。
「role、button、alert、dialog 和地标」这节课中我会学到什么?
应用 ARIA 角色,向辅助技术传达元素用途 你通过在浏览器中直接运行的动手代码来练习 HTML Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 HTML Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 HTML Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「role、button、alert、dialog 和地标」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 HTML Academy 课中编写并运行代码吗?
能。每节 HTML Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 何时使用 ARIA,何时使用原生 HTML
- role、button、alert、dialog 和地标
- aria-label、aria-labelledby 和 aria-describedby
- aria-hidden 和 aria-live