href(Hypertext REFerence)指定鏈接目標地址,用於 <a>、<link>、<area>、<base> 標籤。
| 類型 | 示例 | 說明 |
|---|---|---|
| 絕對 URL | https://example.com/path | 完整地址,跨域跳轉 |
| 相對 URL | /path/page.html、../parent.html | 相對當前域名或目錄 |
| 片段標識符(錨點) | #section1、/page.html#top | 頁面內跳轉,配合 id |
| JavaScript 協議 | javascript:alert(1) | 執行腳本,不推薦,易導致 XSS |
| 郵件協議 | mailto:user@example.com | 喚起郵件客戶端 |
| 電話協議 | tel:+86-138-0000-0000 | 移動端喚起撥號 |
/ 開頭:相對域名根目錄。./ 或無前綴:相對當前目錄。../:相對父級目錄。<a href="https://example.com" target="_blank" rel="noopener noreferrer">新標籤頁打開</a>
_blank:新標籤頁,必加 rel="noopener noreferrer" 防止反向訪問 window.opener。_self(默認):當前頁面。_parent / _top:框架頁面導航。rel="external nofollow" 傳遞權重意圖。download 屬性指定文件名。const link = document.querySelector('a');
link.href = newUrl;
// 或
link.setAttribute('href', newUrl);