href(Hypertext REFerence)是 <a>、<link>、<area> 等標籤的核心屬性,用於**指定超連結目標的 URL**。瀏覽器據此決定用戶點擊後跳轉至何處,或加載何種外部資源(如 CSS)。
https://example.com/page.html —— 完整路徑,跨域可用。/path/page.html、../page.html —— 相對當前頁面或站點根目錄。#section-id —— 頁面內部跳轉至 id="section-id" 的元素。mailto:user@example.com、tel:+8613800000000 —— 唤起郵件客戶端或撥號介面。javascript:void(0); 或 javascript:func() —— **不推薦**,易引發安全與維護問題,請改用事件監聽器。<a href="https://example.com" target="_blank" rel="noopener">外部連結</a>
<a href="#chapter2">跳轉至第二章</a>
規定: <a> 必須提供 href 或 name(HTML5 建議用 id 替代 name)。缺省 href 時,該元素不具備鏈接行為,僅作佔位。
<link rel="stylesheet" href="/css/style.css">
<link rel="icon" href="/favicon.ico">
此處 href 指向樣式表、圖標等外部資源,rel 定義關係類型。
<img src="map.jpg" usemap="#map">
<map name="map">
<area shape="rect" coords="0,0,100,100" href="page1.html" alt="區域1">
<area shape="circle" coords="150,150,50" href="page2.html" alt="區域2">
</map>
& 等),**必須進行 URL 編碼**(如 encodeURIComponent())。target="_blank" 時,**務必加上** rel="noopener noreferrer",防止反向標籤頁竊取 window.opener 引發釣魚攻擊。rel="nofollow"(如付費/不可信連結)。徹底理解 href 的取值範圍、適用標籤及安全規範,是構建健壯、可訪問、SEO 友好 Web 頁面的基石。