示例工程 23-inputs

23-inputs/app/index.html

html · 6.0 KB

运行:输入类型 · text / email / number / date

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>输入类型 · text / email / number / date</title>
    <meta name="description" content="选对 input 类型,让键盘、校验与自动填充一起工作。" />
    <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; }
      label { display: block; margin: 10px 0 4px; font-size: 14px; color: #334155; }
      label:first-of-type { margin-top: 0; }
      input { width: 100%; max-width: 420px; padding: 7px 9px; border: 1px solid #cbd5e1; border-radius: 6px; font: inherit; font-size: 14px; background: #fff; }
      input[type="range"] { padding: 0; }
      button { margin-top: 12px; 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; }
      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="#text">文本类</a>
          <a href="#numeric">数字</a>
          <a href="#date">日期</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 23</p>
        <h1>输入类型</h1>
        <p class="lead">
          输入类型不是装饰:它决定移动端键盘、浏览器校验、自动填充与读屏播报。
        </p>

        <form action="preview.html" method="get">
          <section id="text">
            <h2>1. 文本类</h2>
            <label for="email">邮箱</label>
            <input id="email" name="email" type="email" autocomplete="email" placeholder="you@example.com" />

            <label for="phone">电话</label>
            <input id="phone" name="phone" type="tel" autocomplete="tel" inputmode="tel" placeholder="138 0000 0000" />

            <label for="site">网址</label>
            <input id="site" name="site" type="url" inputmode="url" placeholder="https://example.com" />

            <label for="query">搜索</label>
            <input id="query" name="q" type="search" placeholder="输入关键词" />

            <label for="password">密码</label>
            <input id="password" name="password" type="password" autocomplete="current-password" />
          </section>

          <section id="numeric">
            <h2>2. 数字</h2>
            <label for="age">年龄(18–120)</label>
            <input id="age" name="age" type="number" min="18" max="120" step="1" value="28" />

            <label for="satisfaction">满意度(0–100,步长 5)</label>
            <input id="satisfaction" name="satisfaction" type="range" min="0" max="100" step="5" value="80" />
          </section>

          <section id="date">
            <h2>3. 日期与颜色</h2>
            <label for="start">开始日期</label>
            <input id="start" name="start" type="date" min="2026-01-01" max="2026-12-31" value="2026-11-28" />

            <label for="meeting">会议时间</label>
            <input id="meeting" name="meeting" type="time" min="09:00" max="18:00" value="10:30" />

            <label for="accent">主题色</label>
            <input id="accent" name="accent" type="color" value="#2563eb" />

            <button type="submit">提交预览</button>
          </section>
        </form>

        <section id="rules">
          <h2>4. 规则</h2>
          <ul>
            <li>邮箱用 <code>type="email"</code>,电话用 <code>tel</code>,网址用 <code>url</code></li>
            <li>数字用 <code>number</code> / <code>range</code>,并写 <code>min</code> / <code>max</code> / <code>step</code></li>
            <li>日期用 <code>date</code> / <code>time</code>,值用 ISO 格式</li>
            <li>用 <code>autocomplete</code> 帮助自动填充</li>
            <li>用 <code>inputmode</code> 提示移动端键盘</li>
            <li>类型只做提示,服务端仍要校验</li>
          </ul>
          <p class="note">类型选错,用户会在手机上多打很多字,也可能提交格式错误的数据。</p>
        </section>
      </main>

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