示例工程 46-progressive-enhancement
46-progressive-enhancement/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>渐进增强 · 先能用,再好用</title>
<meta name="description" content="没有 JavaScript 也能筛选与阅读;有 JavaScript 时再加速。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 30px 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%, 800px); 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: 22px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 10px; font-size: clamp(30px, 6vw, 42px); line-height: 1.15; }
.lead { margin: 0 0 18px; color: #334155; font-size: 17px; }
section { margin-top: 22px; padding-top: 16px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 21px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
form { display: flex; flex-wrap: wrap; align-items: end; gap: 8px; padding: 12px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
label { display: block; font-size: 13px; font-weight: 600; }
input[type="search"] { width: 220px; max-width: 100%; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; }
button { padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
button.ghost { border-color: #cbd5e1; background: #fff; color: #0f172a; }
#quick-wrap { padding: 8px 10px; border: 1px dashed #93c5fd; border-radius: 8px; background: #eff6ff; }
#quick-wrap[hidden] { display: none; }
.tasks { margin: 12px 0 0; padding: 0; list-style: none; }
.tasks li { display: flex; align-items: baseline; gap: 8px; padding: 9px 12px; border: 1px solid #e2e8f0; border-radius: 8px; background: #fff; }
.tasks li + li { margin-top: 6px; }
.tasks li[hidden] { display: none; }
.tasks .status { margin-left: auto; padding: 0 8px; border-radius: 999px; background: #f1f5f9; color: #475569; font-size: 12px; white-space: nowrap; }
.tasks .status.done { background: #dcfce7; color: #166534; }
.count { color: #64748b; font-size: 13px; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
details { margin: 10px 0; padding: 10px 12px; border: 1px solid #e2e8f0; border-radius: 8px; background: #fff; }
summary { cursor: pointer; font-weight: 600; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
footer { margin-top: 24px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
/* 只有增强后才启用的样式 */
html[data-enhanced="true"] #fallback-hint { display: none; }
@media (max-width: 560px) {
body { padding: 18px 14px 32px; }
.site-header { flex-direction: column; }
form { align-items: stretch; }
input[type="search"] { width: 100%; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">PE / 2026</span>
<nav aria-label="页内导航">
<a href="#baseline">基线</a>
<a href="#enhance">增强</a>
<a href="#rules">规则</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 46</p>
<h1>渐进增强</h1>
<p class="lead">
先用 HTML 把任务做完,再用 JavaScript 让它更快。脚本失败时,页面不能变成空白。
</p>
<section id="baseline">
<h2>1. 基线:表单筛选</h2>
<p>这个表单没有脚本也能提交。没有 JavaScript 时,它会把查询参数带回页面,由服务端筛选。</p>
<form id="filter-form" method="get" action="#tasks">
<div>
<label for="q">关键词</label>
<input id="q" name="q" type="search" value="" placeholder="例如:文档" />
</div>
<button type="submit">筛选</button>
<button type="reset" class="ghost">清除</button>
</form>
<p class="count" id="count">共 5 项</p>
<p id="fallback-hint" class="note">当前是基线模式:提交后由服务端筛选;启用 JavaScript 后可以即时过滤。</p>
<ul class="tasks" id="tasks">
<li data-title="发布文档"><span>发布文档</span><span class="status">待处理</span></li>
<li data-title="检查构建"><span>检查构建</span><span class="status done">已完成</span></li>
<li data-title="更新截图"><span>更新截图</span><span class="status">待处理</span></li>
<li data-title="审校文案"><span>审校文案</span><span class="status">待处理</span></li>
<li data-title="归档旧版本"><span>归档旧版本</span><span class="status done">已完成</span></li>
</ul>
</section>
<section id="enhance">
<h2>2. 增强:即时过滤</h2>
<p>脚本加载后,多出一个「快速筛选」输入框,输入即过滤;表单也改为不刷新。</p>
<div id="quick-wrap" hidden>
<label for="quick">快速筛选(需要 JavaScript)</label>
<input id="quick" type="search" placeholder="输入关键词" />
</div>
<p class="note">增强只增加速度和便利,不改变基线的可完成任务。</p>
</section>
<section id="rules">
<h2>3. 规则</h2>
<ul>
<li>先写可用的 HTML,再写脚本。</li>
<li>关键内容与操作不依赖脚本。</li>
<li>用特性检测,不要猜浏览器。</li>
<li>脚本失败时不要留下空白或死按钮。</li>
<li>用 <code>noscript</code> 说明降级方式。</li>
<li>增强后仍保留原生语义与键盘行为。</li>
</ul>
<details>
<summary>无 JavaScript 时会怎样</summary>
<p>列表仍可阅读,表单仍可提交,页面不会空白。只有即时过滤与计数更新不可用。</p>
</details>
</section>
</main>
<footer>
<p>示例数据为虚构,仅用于教学。</p>
</footer>
</div>
<noscript>
<p style="max-width:800px;margin:0 auto 20px;padding:10px 14px;border-left:4px solid #d97706;background:#fffbeb;color:#92400e;font-size:14px">
即时筛选需要 JavaScript。你仍可以用上方表单提交关键词,由服务端返回结果。
</p>
</noscript>
<script>
document.documentElement.dataset.enhanced = "true";
const form = document.getElementById("filter-form");
const quickWrap = document.getElementById("quick-wrap");
const quick = document.getElementById("quick");
const count = document.getElementById("count");
const list = document.getElementById("tasks");
const items = [...list.querySelectorAll("li")];
const query = document.getElementById("q");
quickWrap.hidden = false;
const applyFilter = (text) => {
const keyword = text.trim().toLowerCase();
let visible = 0;
for (const item of items) {
const title = (item.dataset.title ?? "").toLowerCase();
const match = !keyword || title.includes(keyword) || item.textContent.toLowerCase().includes(keyword);
item.hidden = !match;
if (match) visible += 1;
}
count.textContent = keyword ? "匹配 " + visible + " 项(共 " + items.length + " 项)" : "共 " + items.length + " 项";
};
quick.addEventListener("input", () => {
query.value = quick.value;
applyFilter(quick.value);
});
form.addEventListener("submit", (event) => {
event.preventDefault();
applyFilter(query.value);
quick.value = query.value;
quick.focus();
});
form.addEventListener("reset", () => {
setTimeout(() => {
quick.value = "";
applyFilter("");
}, 0);
});
applyFilter("");
</script>
</body>
</html>