萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML5 標籤教程:語義化結構、表單增強、多媒體、Canvas 與核心 API 實戰

HTML5 核心價值

  • 語義化: 引入 `
    `、`
    `、`
  • 表單增強: 22 種 `input type`、原生驗證 (`required`、`pattern`、`min/max`)、`datalist`、`output`、`progress`、`meter`。
  • 原生多媒體: `
  • 圖形繪製: ``(即時位圖)、內嵌 ``(矢量保真)。
  • 本地存儲: `localStorage`/`sessionStorage` (5-10MB)、`IndexedDB` (大量結構化)、`Cache Storage` (Service Worker)。
  • 豐富 API: Geolocation、Drag&Drop、Web Workers、WebSocket、SSE、Fetch、Streams、IntersectionObserver、ResizeObserver、Clipboard、File System Access、Web Share、Contact Picker、Payment Request、Credential Management... 持續擴展。

語義化結構實戰:典型文章頁

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
  <meta charset="UTF-8">
  <title>HTML5 語義化結構實戰 | 我的部落格</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="深度解析 HTML5 語義化標籤在實際頁面中的應用模式...">
  <link rel="stylesheet" href="/css/main.css">
</head>
<body>
  <header class="site-header">
    <nav class="main-nav" aria-label="主導航">
      <ul><li><a href="/">首頁</a></li><li><a href="/articles">文章</a></li></ul>
    </nav>
  </header>

  <main id="main-content">
    <article>
      <header>
        <h1>HTML5 語義化結構實戰指南</h1>
        <div class="meta">
          <time datetime="2024-01-15">2024 年 1 月 15 日</time>
          <address>作者:<a rel="author" href="/author/zhangsan">張三</a></address>
        </div>
      </header>

      <figure>
        <img src="/img/html5-semantic.jpg" alt="HTML5 語義化標籤關係圖" width="800" height="450" loading="lazy">
        <figcaption>圖 1:HTML5 核心語義化標籤巢狀關係</figcaption>
      </figure>

      <section aria-labelledby="sec-structure">
        <h2 id="sec-structure">頁面級結構標籤</h2>
        <p>...</p>
      </section>

      <section aria-labelledby="sec-form">
        <h2 id="sec-form">增強型表單</h2>
        <p>...</p>
      </section>

      <footer>
        <p>© 2024 張三. 采用 CC BY-NC 4.0 授權.</p>
      </footer>
    </article>

    <aside aria-labelledby="aside-title">
      <h2 id="aside-title">相關推薦</h2>
      <nav>
        <ul><li><a href="/article/html5-forms">HTML5 表單深度解析</a></li></ul>
      </nav>
    </aside>
  </main>

  <footer class="site-footer">
    <p>© 2024 My Blog. All rights reserved.</p>
  </footer>
</body>
</html>

增強型表單實戰:註冊頁

<form id="register-form" novalidate>
  <div class="field">
    <label for="email">電子郵箱</label>
    <input type="email" id="email" name="email" required autocomplete="email" placeholder="you@example.com">
    <span class="error" aria-live="polite"></span>
  </div>

  <div class="field">
    <label for="password">密碼</label>
    <input type="password" id="password" name="password" required minlength="8" autocomplete="new-password">
    <span class="hint">至少 8 位,含大小寫、數字、特殊符號</span>
  </div>

  <div class="field">
    <label for="birthdate">出生日期</label>
    <input type="date" id="birthdate" name="birthdate" max="2006-12-31">
  </div>

  <div class="field">
    <label for="avatar">頭像</label>
    <input type="file" id="avatar" name="avatar" accept="image/*" capture="environment">
  </div>

  <div class="field">
    <label for="theme">主題色</label>
    <input type="color" id="theme" name="theme" value="#3b82f6">
  </div>

  <button type="submit">創建賬號</button>
</form>

<script>
const form = document.getElementById('register-form');
form.addEventListener('submit', e => {
  if (!form.checkValidity()) { e.preventDefault(); form.reportValidity(); return; }
  const fd = new FormData(form);
  fetch('/api/register', { method:'POST', body:fd })
    .then(r => r.json())
    .then(data => { /* 跳轉或提示 */ });
});
</script>

多媒體最佳實踐

<!-- 視頻:多編碼回退 + 字幕 + 預載策略 + 響應式 -->
<video controls preload="metadata" poster="/img/poster.webp" width="1280" height="720">
  <source src="/media/video.av1.mp4" type="video/mp4; codecs=av01.0.05M.08">
  <source src="/media/video.hevc.mp4" type="video/mp4; codecs=hev1.1.6.L93.90">
  <source src="/media/video.vp9.webm" type="video/webm; codecs=vp09.00.10.08">
  <source src="/media/video.h264.mp4" type="video/mp4">
  <track kind="subtitles" src="/media/video.zh.vtt" srclang="zh" label="中文" default>
  <track kind="subtitles" src="/media/video.en.vtt" srclang="en" label="English">
  您的瀏覽器不支援 HTML5 視頻播放。
</video>

<!-- 響應式圖片:Art Direction + 格式回退 -->
<picture>
  <source media="(max-width: 480px)" srcset="/img/hero-mobile.avif" type="image/avif">
  <source media="(max-width: 480px)" srcset="/img/hero-mobile.webp" type="image/webp">
  <source srcset="/img/hero-desktop.avif" type="image/avif">
  <source srcset="/img/hero-desktop.webp" type="image/webp">
  <img src="/img/hero-desktop.jpg" alt="Hero 圖片" width="1920" height="1080" loading="eager" fetchpriority="high">
</picture>

Canvas 繪圖入門

<canvas id="chart" width="600" height="300" aria-label="銷售趨勢圖:Q1 120, Q2 150, Q3 180, Q4 210">
  不支援 Canvas 的瀏覽器請升級。
</canvas>

<script>
const canvas = document.getElementById('chart');
const ctx = canvas.getContext('2d');
const data = [120, 150, 180, 210];
const max = Math.max(...data);
const barW = 80, gap = 40, bottom = 30, left = 50;

ctx.font = '14px system-ui';
ctx.fillStyle = '#374151';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';

data.forEach((val, i) => {
  const h = (val / max) * (canvas.height - bottom - 40);
  const x = left + i * (barW + gap) + barW / 2;
  const y = canvas.height - bottom - h;

  // 柱狀
  ctx.fillStyle = '#3b82f6';
  ctx.fillRect(x - barW/2, y, barW, h);

  // 數值
  ctx.fillStyle = '#111827';
  ctx.fillText(val, x, y - 20);

  // 季度標籤
  ctx.fillText(`Q${i+1}`, x, canvas.height - bottom + 10);
});
</script>

HTML5 學習進階路線圖

  1. 語義化重構: 找一個舊版 `
    ` 滿天飛的頁面,重寫為語義化結構,驗證 W3C Validator 通過。
  2. 表單挑戰: 實作完整註冊/登錄/設置流程,僅用原生驗證 + 少量 JS 增強,無第三方庫。
  3. 多媒體實戰: 搭建視頻播放頁(HLS/DASH 自適應碼流、字幕、畫中畫、鍵盤控制)。
  4. Canvas 進階: 學習路徑繪製、圖片處理、像素操作、動畫循環、WebGL 入門。
  5. API 專項攻克: 每週選一個新 API (IntersectionObserver, File System Access, Web Share...) 做 Demo。
  6. PWA 實戰: Service Worker + Web App Manifest + 離線緩存 + 推送通知 + 安裝提示。

HTML5 重新定義了 Web 平台能力邊界。系統學習、大量實戰、持續跟進 Living Standard,才能駕馭現代 Web 開發的全棧挑戰。

copyright © 萬盛學電腦網 all rights reserved