超文本鏈接是 Web 互聯互通的基石。掌握 URL 結構、錨點導航、目標窗口控制、圖像映射,構建豐富導航體驗。
protocol://host[:port]/path/to/resource
scheme://user:pass@host:port/path?query#fragment
| 組件 | 說明 | 範例 |
|---|---|---|
| scheme (protocol) | 訪問協議 | http, https, ftp, mailto, tel, file |
| host | 主機域名或 IP | www.example.com, 192.168.1.1 |
| port | 端口號(可省略預設) | :80, :443, :8080 |
| path | 資源路徑 | /products/item.html |
| query | 查詢參數 | ?id=123&color=red |
| fragment | 頁面內錨點 | #section2 |
絕對 URL** 包含 scheme+host,**相對 URL** 相對當前文檔路徑。同目錄資源優先用相對 URL,利於遷移維護。
<a><a href="https://example.com">訪問示例網站</a>
<a href="/about.html">關於我們</a> <!-- 站內絕對路徑 -->
<a href="../contact.html">聯繫我們</a> <!-- 相對路徑 -->
<!-- 標記目標位置 -->
<h2 id="section2">第二節</h2> <!-- 現代:id 屬性 -->
<a name="section2"></a> <!-- 舊式:name 屬性 -->
<!-- 跳轉鏈接 -->
<a href="#section2">跳轉到第二節</a> <!-- 同頁面 -->
<a href="page.html#section2">去頁面第二節</a> <!-- 跨頁面 -->
HTML5 推薦用 **`id`** 替代 `name`,通用於所有元素。
target| 值 | 行為 |
|---|---|
| `_self` | 當前框架/窗口(預設) |
| `_blank` | **新分頁/窗口**(必加 `rel="noopener noreferrer"` 防安全風險) |
| `_parent` | 父框架(框架集中) |
| `_top` | 頂層窗口(跳出所有框架) |
| `framename` | 指定名稱的框架/iframe |
<a href="https://example.com">
<img src="banner.jpg" alt="橫幅廣告">
</a>
`alt` 屬性**必填**(無障礙/SEO/圖片失效時顯示)。
<a href="/cgi-bin/imagemap/map.map">
<img src="map.gif" ismap>
</a>
<img src="map.gif" usemap="#navmap" alt="導航地圖">
<map name="navmap">
<area shape="rect" coords="10,10,100,50" href="/home" alt="首頁">
<area shape="circle" coords="150,150,30" href="/about" alt="關於">
<area shape="poly" coords="200,10 250,50 200,90" href="/contact" alt="聯繫">
<area shape="default" href="/sitemap" alt="網站地圖">
</map>
鏈接編織知識圖譜,優雅的鏈接設計是用戶體驗與 SEO 的雙贏。