<a> 元素構成了萬維網的拓撲結構。掌握其屬性、URL 類型、錨點導航、郵件/電話協議、無障礙屬性,是前端開發的入門必修課。本文從規範角度系統講解,並給出生產級最佳實踐清單。
<a href="https://example.com/page.html">鏈接文本</a>
href (Hypertext REFerence):目標地址,**必填**,否則非真正鏈接(僅佔位)。transparent,可包裹流式內容(文本、圖片、段落、甚至區塊級元素 HTML5 允許)。| 類型 | 格式 | 示例 | 適用場景 |
|---|---|---|---|
| 絕對 URL | scheme://host/path?query#hash | https://example.com/docs?id=1#sec2 | 跨域、外部資源、SEO 規範 |
| 相對 URL (相對當前文檔) | path/to/file.html | about.html, sub/page.html | 同站內部鏈接,遷移域名無需改動 |
| 相對根路徑 | /path/to/file.html | /products/item.html | 站點內跨目錄跳轉,部署子目錄時需注意 |
| 協議相對 URL | //example.com/path | //cdn.example.com/asset.js | 自動匹配當前頁協議 (HTTP/HTTPS),**現代最佳實踐已棄用**,改用絕對 HTTPS |
| 錨點/片段 | #id 或 url#id | #section3, /article#comments | 頁面內跳轉、SPA 路由歷史模式 |
| mailto | mailto:email@example.com?subject=...&body=... | mailto:support@example.com?subject=Feedback | 喚起默認郵件客戶端 |
| tel | tel:+86-138-0000-0000 | tel:+8613800000000 | 移動端喚起撥號面板 |
| sms | sms:+8613800000000?body=... | sms:10086?body=CXLL | 喚起短信編輯 |
| javascript: | javascript:code() | javascript:void(0) | 極不推薦,CSP 策略常拦截,改用事件監聽器 |
| data: | data:image/png;base64,... | 內聯小圖標 | 減少 HTTP 請求,增加 HTML 體積 |
| 屬性 | 值類型 | 作用 | 備註 |
|---|---|---|---|
| target | _blank _self _parent _top framename | 鏈接打開上下文 | _blank **必配** rel="noopener noreferrer" 防逆向標籤劫持 |
| rel | 空格分隔關鍵字 | 當前文檔與目標關系 | noopener 斷開 opener 引用,noreferrer 隱藏 Referer,nofollow SEO 不傳權重,external 外部鏈接語義 |
| download | 可選文件名 | 強制下載而非導航 | 同源策略限制,跨域需 Content-Disposition 配合 |
| hreflang | BCP 47 語言標簽 | 目標文檔語言 | SEO 多語言站點 hreflang 標記輔助 |
| type | MIME 類型 | 目標資源類型提示 | 瀏覽器預加載優化參考 |
| referrerpolicy | no-referrer origin strict-origin-when-cross-origin... | 控制 Referer 頭發送策略 | 隱私合規關鍵 |
| ping | 空格分隔 URL 列表 | 點擊時發送 POST 到指定 URL (信標) | 統計點擊無需跳轉中間頁,隱私爭議大,謹慎用 |
| id / class / style / title / data-* | 通用屬性 | 標識、樣式、工具提示、自定義數據 | title 懸停提示,移動端不可見,重要信息勿依賴 |
<nav>
<a href="#intro">簡介</a> |
<a href="#features">特性</a> |
<a href="#pricing">價格</a>
</nav>
<section id="intro"><h2>簡介</h2>...</section>
<section id="features"><h2>特性</h2>...</section>
<section id="pricing"><h2>價格</h2>...</section>
id 必須唯一、以字母開頭、區分大小寫。scroll-behavior: smooth 實現平滑滾動,無需 JS。:target 偽類可高亮當前錨點區塊。<a href="..."><img src="logo.svg" alt="公司名"></a>,alt 傳達鏈接目的。aria-label、aria-describedby、aria-expanded (下拉菜單)。:focus-visible { outline: 2px solid #0066cc; } 確保鍵盤導航可見。<a href="#main" class="skip-link">跳到主內容</a>,視隱用戶快速跳過導航。<!-- 外部鏈接標準寫法 -->
<a href="https://external.com" target="_blank" rel="noopener noreferrer nofollow">外部站點</a>
<!-- 下載文件 -->
<a href="/assets/report.pdf" download="2024-Q1-Report.pdf" type="application/pdf">下載報告</a>
<!-- 隱私保護:不發送 Referer -->
<a href="https://partner.com" referrerpolicy="no-referrer">合作夥伴</a>
a {
color: #0066cc;
text-decoration: none;
transition: color 0.2s, border-color 0.2s;
}
a:hover { color: #004499; text-decoration: underline; }
a:active { color: #cc0000; }
a:focus-visible { outline: 2px solid #0066cc; outline-offset: 2px; }
a:visited { color: #663399; } /* 隱私限制:僅允許顏色屬性 */
<a> 內嵌 <a>、<button>、<input> 等交互元素,會導致 DOM 解析錯誤。href="" 刷新當前頁,href="#" 跳轉頂部。無導航意圖請用 <button> 或 <span role="button" tabindex="0">。//example.com 在 HTTPS 頁面加載 HTTP 資源會觸發混合內容警告,**統一使用 https://**。<a> 看似簡單,實則承載導航、語義、無障礙、安全、SEO 多重職責。遵循「語義優先、屬性完備、無障礙友好、安全最小權限」原則,能構建健壯、可維護、用戶友好的超鏈接網絡。