href 核心作用
href (Hypertext REFerence) 指定鏈接目標地址。若缺失 href,<a> 不是超鏈接,僅是佔位符(可作錨點目標 name/id)。
支持的 URL 類型
| 類型 | 示例 | 說明 |
| 絕對 URL | https://example.com/path | 完整協議+域名+路徑 |
| 相對 URL | /path/page.html, ../sibling.html | 相對當前頁/域名根目錄 |
| 片段標識符 | #section1, /page.html#top | 頁內錨點跳轉 |
| 伪協議 | mailto:user@ex.com, tel:+8613800000000, sms: | 調用本地應用 |
| JavaScript | javascript:void(0), javascript:doSomething() | 強烈不推薦,阻礙無障礙/SEO/CSP |
| Data URI | data:text/html,<h1>Hi</h1> | 內嵌小資源 |
| Blob/Filesystem | blob:https://example.com/uuid | 臨時對象 URL |
關鍵輔助屬性
| 屬性 | 值/說明 | 作用 |
target | _blank (新標籤), _self (默認), _parent, _top, iframe名 | 打開上下文 |
rel | noopener (斬斷 opener), noreferrer (不發 Referer), nofollow (SEO 不傳權重), external, ugc, sponsored | 安全/SEO 關係聲明 |
download | filename.ext | 強制下載而非導航 |
hreflang | zh-Hant, en | 目標語言提示 |
type | text/html, application/pdf | MIME 類型提示 |
ping | https://analytics.com/click | 點擊上報(空格分隔多 URL) |
referrerpolicy | no-referrer, origin, strict-origin-when-cross-origin | 控制 Referer 頭 |
安全最佳實踐
<!-- 外部鏈接新標籤必加 -->
<a href="https://external.com" target="_blank" rel="noopener noreferrer">外部站點</a>
<!-- 用戶生成內容(UGC)鏈接 -->
<a href="https://user-site.com" rel="ugc nofollow">用戶鏈接</a>
<!-- 廣告/贊助鏈接 -->
<a href="https://ad.com" rel="sponsored nofollow">贊助鏈接</a>
noopener 防止 window.opener 被惡意竄改(釣魚、重定向)。
noreferrer 隱藏 Referer,保護隱私。
- 避免
javascript: 協議,改用 <button> + 事件監聽器。
SEO 影響
- 錨文本(鏈接文字)是核心排名信號,需自然、描述性、含關鍵詞。
rel="nofollow" 告訴搜索引擎不傳遞 PageRank(Google 現視為「提示」而非指令)。
- 死鏈、重定向鏈、過多無關外鏈損害權重。
- 使用
<a> 而非 onclick="location.href=..." 確保爬蟲可發現。