示例工程 24-labels-errors
24-labels-errors/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>标签与错误 · label / aria-describedby / 错误提示</title>
<meta name="description" content="显式标签、隐式标签、帮助文字与错误关联。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 32px 20px 40px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.page { width: min(100%, 760px); margin: 0 auto; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 28px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); line-height: 1.15; }
.lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 22px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
form { margin: 0; padding: 16px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
.field { margin-bottom: 16px; }
label { display: block; margin-bottom: 4px; font-size: 14px; color: #334155; }
.req { color: #b91c1c; }
input { width: 100%; max-width: 420px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; font-size: 14px; background: #fff; }
input[aria-invalid="true"] { border-color: #b91c1c; }
.help { margin: 4px 0 0; color: #64748b; font-size: 13px; }
.error { margin: 4px 0 0; color: #b91c1c; font-size: 13px; }
.error::before { content: "错误:"; font-weight: 600; }
.summary { margin: 0 0 16px; padding: 12px 14px; border: 1px solid #fecaca; border-left: 4px solid #b91c1c; border-radius: 6px; background: #fef2f2; color: #7f1d1d; font-size: 14px; }
.summary ul { margin: 6px 0 0; padding-left: 20px; color: inherit; }
.summary a { color: #7f1d1d; }
.check { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 4px; }
.check input { width: auto; margin-top: 4px; }
.check label { margin: 0; }
button { margin-top: 4px; padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; font-size: 14px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
footer { margin-top: 30px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
body { padding: 20px 14px 32px; }
.site-header { flex-direction: column; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">FORM NOTES</span>
<nav aria-label="页内导航">
<a href="#labels">标签</a>
<a href="#errors">错误</a>
<a href="#rules">规则</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 24</p>
<h1>标签与错误</h1>
<p class="lead">
标签告诉用户「这是什么」,帮助文字告诉用户「怎么填」,错误提示告诉用户「哪里错了」。
</p>
<section id="labels">
<h2>1. 标签</h2>
<form action="preview.html" method="post" novalidate>
<div class="field">
<label for="name">称呼 <span class="req" aria-hidden="true">*</span></label>
<input id="name" name="name" type="text" autocomplete="name" required aria-describedby="name-help" />
<p class="help" id="name-help">填写你希望我们使用的称呼。</p>
</div>
<div class="field">
<label for="email">邮箱 <span class="req" aria-hidden="true">*</span></label>
<input
id="email"
name="email"
type="email"
autocomplete="email"
required
aria-invalid="true"
aria-describedby="email-help email-error"
/>
<p class="help" id="email-help">用于登录与接收通知。</p>
<p class="error" id="email-error">请输入有效的邮箱地址。</p>
</div>
<div class="field">
<label>
<input type="checkbox" name="subscribe" />
订阅每周更新
</label>
</div>
<button type="submit">提交</button>
</form>
</section>
<section id="errors">
<h2>2. 错误摘要</h2>
<div class="summary" role="alert" aria-labelledby="summary-title">
<p id="summary-title">提交时发现 1 个问题:</p>
<ul>
<li><a href="#email">邮箱:请输入有效的邮箱地址。</a></li>
</ul>
</div>
<p>摘要放在表单顶部,链接到具体字段;字段本身再用 <code>aria-invalid</code> 与 <code>aria-describedby</code> 关联。</p>
</section>
<section id="rules">
<h2>3. 规则</h2>
<ul>
<li>每个控件都要有可访问名称,优先用 <code>label for</code>。</li>
<li>placeholder 不是标签,输入后就消失。</li>
<li>帮助文字用 <code>aria-describedby</code> 关联。</li>
<li>错误用文字说明,不只靠红色边框。</li>
<li>错误摘要链接到字段,提交失败后聚焦摘要或第一个错误。</li>
<li>必填同时有文字与 <code>required</code>。</li>
</ul>
<p class="note">读屏用户依赖名称、描述与错误状态,缺一项都会让表单变难。</p>
</section>
</main>
<footer>
<p>示例数据为虚构,仅用于教学。</p>
</footer>
</div>
</body>
</html>