示例工程 10-links

10-links/app/index.html

html · 5.3 KB

运行:链接 · 相对、锚点与下载

点击「运行示例」后,左侧显示源码、右侧显示运行结果;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="相对链接、锚点链接、下载链接与安全属性的最小示例。" />
    <style>
      * { box-sizing: border-box; }
      body {
        margin: 0;
        min-height: 100vh;
        padding: 32px 20px;
        background: #f8fafc;
        color: #0f172a;
        font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
        line-height: 1.75;
      }
      .page { width: min(100%, 780px); margin: 0 auto; }
      .skip { position: absolute; left: -9999px; top: 0; padding: 8px 12px; background: #2563eb; color: #fff; }
      .skip:focus { left: 8px; top: 8px; z-index: 10; }
      .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: 32px 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 24px; color: #334155; font-size: 17px; }
      section { margin-top: 28px; padding-top: 20px; border-top: 1px solid #e2e8f0; }
      h2 { margin: 0 0 10px; font-size: 23px; }
      p { margin: 0 0 12px; color: #334155; }
      ul, ol { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
      li + li { margin-top: 4px; }
      a { color: #2563eb; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
      pre { margin: 10px 0 0; padding: 14px; overflow-x: auto; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-size: 12px; line-height: 1.7; }
      pre code { padding: 0; background: none; color: inherit; }
      .note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
      footer { margin-top: 32px; padding-top: 16px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        .site-header { flex-direction: column; }
        nav a + a { margin-left: 12px; }
      }
    </style>
  </head>
  <body>
    <a class="skip" href="#main">跳到主内容</a>

    <div class="page" id="main">
      <header class="site-header">
        <span class="brand">LINK NOTES</span>
        <nav aria-label="页内导航">
          <a href="#relative">相对链接</a>
          <a href="#anchor">锚点</a>
          <a href="#download">下载</a>
          <a href="#contact">联系</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 10</p>
        <h1>链接</h1>
        <p class="lead">
          链接把页面连成站点。相对、绝对、锚点、下载、邮件,各有各的写法与边界。
        </p>

        <section id="relative">
          <h2>1. 相对链接</h2>
          <p>相对链接基于当前页面地址解析。同目录直接写文件名:</p>
          <p><a href="demo-01-relative-absolute/index.html">打开相对链接示例</a></p>
          <p>上一级写 <code>../</code>,子目录写 <code>docs/setup.html</code>。</p>
          <p>相对链接在本地与部署环境都成立,是站内链接的首选。</p>
        </section>

        <section id="anchor">
          <h2>2. 锚点链接</h2>
          <p>锚点跳到同一页的某个 <code>id</code>:</p>
          <ul>
            <li><a href="#relative">回到相对链接</a></li>
            <li><a href="#download">跳到下载</a></li>
          </ul>
          <p>跳转后目标元素会获得焦点,键盘用户也能继续操作。</p>
        </section>

        <section id="download">
          <h2>3. 下载链接</h2>
          <p>
            <a href="assets/sample.txt" download="link-checklist.txt">下载练习清单(TXT)</a>
          </p>
          <p><code>download</code> 提示浏览器下载而不是直接打开,文件名可以另指定。</p>
          <p>跨源下载可能被浏览器忽略 <code>download</code>,以实际行为为准。</p>
        </section>

        <section id="contact">
          <h2>4. 邮件与电话</h2>
          <p><a href="mailto:hi@example.com?subject=链接问题">发邮件</a>,或拨打 <a href="tel:+8613800000000">138 0000 0000</a>。</p>
          <p>这类链接要写清用途,不要只放图标。</p>
        </section>

        <section id="rules">
          <h2>5. 写法与安全</h2>
          <pre><code>&lt;!-- 绝对 URL 只作为文字示例 --&gt;
https://example.com/docs

&lt;!-- 新窗口打开时加 rel --&gt;
&lt;a href=&#34;https://example.com&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;示例&lt;/a&gt;</code></pre>
          <p class="note">链接文字要能独立说明目标。不要写「点击这里」,也不要用 <code>href="#"</code> 当按钮。</p>
        </section>
      </main>

      <footer>
        <p>示例数据,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>