示例工程 32-details-summary

32-details-summary/app/index.html

html · 5.2 KB

运行:details 与 summary · 原生折叠

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>details 与 summary · 原生折叠</title>
    <meta name="description" content="用 details/summary 做 FAQ、嵌套说明与手风琴。" />
    <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: 26px 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 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: 21px; }
      p { margin: 0 0 10px; color: #334155; }
      details { margin: 10px 0; padding: 10px 14px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
      summary { cursor: pointer; color: #0f172a; font-weight: 600; }
      summary:hover { color: #2563eb; }
      details > p { margin: 10px 0 0; }
      details details { margin: 10px 0 0; background: #f8fafc; }
      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; }
      footer { margin-top: 28px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        body { padding: 18px 14px 32px; }
        .site-header { flex-direction: column; }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header class="site-header">
        <span class="brand">FAQ / 2026</span>
        <nav aria-label="页内导航">
          <a href="#faq">常见问题</a>
          <a href="#nested">嵌套</a>
          <a href="#accordion">手风琴</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 32</p>
        <h1>原生折叠</h1>
        <p class="lead">
          <code>details</code> 与 <code>summary</code> 让内容可展开、可收起,不需要脚本。
        </p>

        <section id="faq">
          <h2>1. 常见问题</h2>

          <details open>
            <summary>details 和 summary 是什么关系?</summary>
            <p><code>details</code> 是折叠容器,<code>summary</code> 是它的标题,必须是第一个子元素。</p>
          </details>

          <details>
            <summary>没有 summary 会怎样?</summary>
            <p>浏览器会显示默认文字「详细信息」,但可读性差,建议总是写 summary。</p>
          </details>

          <details>
            <summary>可以用脚本控制吗?</summary>
            <p>可以,但原生点击、键盘与读屏已经可用,脚本只做增强。</p>
          </details>
        </section>

        <section id="nested">
          <h2>2. 嵌套折叠</h2>
          <details>
            <summary>更多细节</summary>
            <p>外层展开后,可以看到内层问题。</p>
            <details>
              <summary>内层问题</summary>
              <p>嵌套 details 可以表达「问题里的子问题」。</p>
            </details>
          </details>
        </section>

        <section id="accordion">
          <h2>3. 手风琴</h2>
          <p>同一个 <code>name</code> 的 details 一次只展开一个:</p>
          <details name="faq-group" open>
            <summary>第一个问题</summary>
            <p>打开第二个时,第一个会自动收起。</p>
          </details>
          <details name="faq-group">
            <summary>第二个问题</summary>
            <p>这是同组手风琴的第二个面板。</p>
          </details>
          <p class="note">手风琴适合相互排斥的短内容,不适合需要同时对照的长内容。</p>
        </section>

        <section id="rules">
          <h2>4. 规则</h2>
          <ul>
            <li><code>summary</code> 必须是 <code>details</code> 的第一个子元素。</li>
            <li>一个 details 只写一个 summary。</li>
            <li><code>open</code> 控制默认展开。</li>
            <li>折叠内容仍在 DOM 中,搜索可能命中。</li>
            <li>打印时关闭的内容可能不显示,关键信息不要藏在折叠里。</li>
          </ul>
        </section>
      </main>

      <footer>
        <p>示例内容为虚构,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>