示例工程 24-labels-errors
24-labels-errors/demo-01-labels/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>标签演示</title>
<style>
body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
.box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
h1 { margin: 0 0 16px; font-size: 28px; }
h2 { margin: 0 0 8px; font-size: 17px; }
p { margin: 0 0 10px; color: #334155; }
fieldset { margin: 0 0 12px; padding: 10px; border: 1px solid #e2e8f0; border-radius: 8px; }
legend { padding: 0 6px; color: #475569; font-size: 13px; }
label { display: block; margin: 6px 0 4px; font-size: 14px; }
input { width: 100%; max-width: 340px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; }
.help { margin: 4px 0 0; color: #64748b; font-size: 13px; }
.req { color: #b91c1c; }
</style>
</head>
<body>
<h1>标签演示</h1>
<div class="box">
<h2>显式标签</h2>
<label for="name">称呼</label>
<input id="name" name="name" type="text" aria-describedby="name-help" />
<p class="help" id="name-help">填写你希望我们使用的称呼。</p>
</div>
<div class="box">
<h2>隐式标签</h2>
<label class="check">
<input type="checkbox" name="subscribe" />
订阅每周更新
</label>
<p class="help">点击文字也能切换复选框。</p>
</div>
<div class="box">
<h2>分组与必填</h2>
<fieldset>
<legend>联系方式</legend>
<label for="email">邮箱 <span class="req">*</span></label>
<input id="email" name="email" type="email" required />
<p class="help">必填项同时有可见星号与 required 属性。</p>
</fieldset>
</div>
</body>
</html>