HTML 檔案 (副檔名 .html 或 .htm) 是純文字文件,可用瀏覽器預覽渲染結果,或用編輯器修改原始碼。本文提供各作業系統、各需求的完整開啟方式。
.html 檔案。# Windows (PowerShell/CMD)
start index.html
# 或指定瀏覽器
start chrome index.html
start msedge index.html
start firefox index.html
# macOS
open index.html
open -a "Google Chrome" index.html
open -a "Firefox" index.html
# Linux
xdg-open index.html
google-chrome index.html
firefox index.html
code index.html — 免費、開源、擴充豐富、內建 Emmet、Git、終端機、Live Server 擴充即時預覽。subl index.html — 極速、多游標、命令面板、需授權。nvim index.html — 終端機模態編輯器,學習曲線陡但效率極高。file:///sdcard/Download/index.html (路徑視實際儲存位置調整)。| 問題 | 原因 | 解決方法 |
|---|---|---|
| 瀏覽器顯示原始碼而非渲染畫面 | 檔案副檔名錯誤 (.txt) 或 HTTP 回應 Content-Type 錯誤 | 確認副檔名為 .html;本地檔案瀏覽器通常能自動判斷 |
| 中文亂碼 | 檔案編碼非 UTF-8,或缺少 <meta charset="UTF-8"> | 編輯器另存為 UTF-8 (無 BOM);確保 head 首行有 charset meta |
| 圖片/CSS/JS 無法載入 | 相對路徑錯誤、本地檔案跨域限制 (file:// 協定) | 檢查路徑;啟動本地伺服器 (VS Code Live Server, npx serve, python -m http.server) |
| 雙擊用錯誤程式開啟 | 檔案關聯設定錯誤 | 右鍵 → 開啟舊檔案 → 選擇其他應用程式 → 勾選「永遠使用此應用程式開啟 .html 檔案」 |
| 手機無法開啟本地 HTML | 瀏覽器限制 file:// 存取、路徑錯誤 | 用檔案管理App分享給瀏覽器;或架設本地 HTTP 伺服器 (Termux + python/http-server) |
index.html → Open with Live Server。https://127.0.0.1:5500,**儲存即自動重整**,支援 CSS/JS 熱更新。! 按 Tab 產生 HTML5 骨架)。選擇適合自己習慣的工具鏈,讓「寫 code → 看結果」迴路最短,是提升前端開發效率的關鍵。