<a href="URL">連結文字</a>
href (Hypertext Reference):目標資源位址(網頁、圖片、影片、PDF、任意檔案)。| 屬性 | 用途 | 範例 |
|---|---|---|
href | 目標網址 | https://example.com、/path/page.html、#section |
target | 開啟位置 | _blank (新分頁)、_self (同頁)、_parent、_top、iframeName |
title | 滑鼠懸停提示文字 | title="前往首頁"、多行用
換行 |
rel | 關聯類型(SEO/安全) | noopener、noreferrer、nofollow、external |
download | 強制下載而非開啟 | download="report.pdf" (HTML5) |
hreflang | 目標語言代碼 | hreflang="en" |
type | 目標 MIME 類型 | type="application/pdf" |
<a href="https://www.example.com" target="_blank" rel="noopener noreferrer">Example 網站</a>
安全提醒: target="_blank" **必須**搭配 rel="noopener noreferrer",防止新頁面透過 window.opener 竄改原頁面(Tabnabbing 攻擊)。
<!-- 目標位置 -->
<h2 id="chapter1">第一章</h2>
<!-- 連結 -->
<a href="#chapter1">跳至第一章</a>
HTML5 建議使用 id 而非 name 定義錨點。
<a href="mailto:contact@example.com?subject=詢問&body=內容預設">聯繫我們</a>
cc、bcc、subject、body 參數(需 URL 編碼)。<a href="tel:+886-2-12345678">致電客服</a>
<a href="sms:+886912345678?body=你好">發送簡訊</a>
<a href="/files/report.pdf" download="2024年報.pdf">下載年報</a>
同源策略限制:跨域資源需後端設定 Content-Disposition: attachment 配合。
aria-label="外部連結,新分頁開啟" 提示。<nav> 內,結構化資料 SiteNavigationElement。// 同頁跳轉平滑捲動
document.querySelector('a[href^="#"]').addEventListener('click', e => {
e.preventDefault();
document.querySelector(e.target.getAttribute('href')).scrollIntoView({behavior: 'smooth'});
});
// 確認離開
<a href="delete.php" onclick="return confirm('確定刪除?')">刪除</a>