萬盛學電腦網

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

常見 HTML5 標簽教程:語義化、表單增強、多媒體與 API 實戰

HTML5 核心升級點

  • 語義化標籤: `
    `、`
    `、`
  • 增強型表單: 新增 `type="email|url|tel|number|date|color|range|search"`,驗證屬性 `required`、`pattern`、`min/max/step`、`placeholder`、`autocomplete`。
  • 原生多媒體: `
  • 圖形繪製: ``(即時位圖)、``(矢量,可內嵌 DOM)。
  • 本地存儲: `localStorage`、`sessionStorage`、`IndexedDB`。
  • 新 API: Geolocation、Drag & Drop、Web Workers、WebSocket、Service Worker、Fetch、IntersectionObserver 等。

語義化結構標籤實戰佈局

<body>
  <header>
    <nav><ul><li><a href="/">首頁</a></li>...</ul></nav>
  </header>

  <main>
    <article>
      <header><h1>文章標題</h1><time datetime="2024-01-15">1 月 15 日</time></header>
      <section><h2>章節一</h2><p>內容...</p></section>
      <section><h2>章節二</h2><p>內容...</p></section>
      <footer><address>作者:張三</address></footer>
    </article>

    <aside>
      <section><h3>相關推薦</h3><ul>...</ul></section>
    </aside>
  </main>

  <footer>© 2024 My Site</footer>
</body>

增強型表單實戰

<form novalidate>
  <div>
    <label for="email">電子郵箱</label>
    <input type="email" id="email" name="email" required placeholder="you@example.com" autocomplete="email">
  </div>
  <div>
    <label for="age">年齡 (18-99)</label>
    <input type="number" id="age" name="age" min="18" max="99" step="1" required>
  </div>
  <div>
    <label for="color">主題色</label>
    <input type="color" id="color" name="color" value="#2563eb">
  </div>
  <div>
    <label for="avatar">頭像</label>
    <input type="file" id="avatar" name="avatar" accept="image/*">
  </div>
  <button type="submit">註冊</button>
</form>

<script>
document.querySelector('form').addEventListener('submit', e => {
  if (!e.target.checkValidity()) { e.preventDefault(); e.target.reportValidity(); }
});
</script>

多媒體嵌入最佳實踐

<!-- 視頻:多格式回退、字幕、預載、響應式 -->
<video controls preload="metadata" poster="/img/poster.jpg" width="640" height="360">
  <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.webm" type="video/webm; codecs=vp9">
  <source src="/media/video.mp4" type="video/mp4">
  <track kind="subtitles" src="/media/video.vtt" srclang="zh" label="中文" default>
  您的瀏覽器不支援 HTML5 視頻。
</video>

<!-- 音頻 -->
<audio controls preload="none">
  <source src="/media/audio.opus" type="audio/ogg; codecs=opus">
  <source src="/media/audio.mp3" type="audio/mpeg">
</audio>

Canvas 基礎繪圖

<canvas id="chart" width="600" height="300">不支援 Canvas</canvas>
<script>
const ctx = document.getElementById('chart').getContext('2d');
ctx.fillStyle = '#2563eb';
ctx.fillRect(50, 50, 200, 100);
ctx.beginPath();
ctx.arc(400, 150, 60, 0, Math.PI * 2);
ctx.fillStyle = '#22c55e';
ctx.fill();
</script>

HTML5 學習路徑建議

  1. 語義化標籤 → 替換無意義 `
    `,重構舊頁面練習。
  2. 表單新屬性/類型 → 實作註冊/登錄/設置頁,體驗原生驗證。
  3. 多媒體 → 嵌入視頻/音頻,處理格式兼容、字幕、預載策略。
  4. Canvas/SVG → 繪製簡單圖表、動畫、遊戲原型。
  5. API 專項:Geolocation(定位)、Drag&Drop(拖拽上傳)、Web Workers(計算卸載)、Service Worker(離線/PWA)。
  6. 規範文檔:HTML Living Standard、MDN Web Docs。

HTML5 不只是「新增幾個標籤」,而是 Web 平台能力的質變。系統學習並實戰應用,才能發揮現代 Web 的全部潛力。

copyright © 萬盛學電腦網 all rights reserved