萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML <a> 標籤 href 屬性深度解析:URL 類型、鏈接行為、安全屬性與 SEO 影響

href 核心作用

href (Hypertext REFerence) 指定鏈接目標地址。若缺失 href,<a> 不是超鏈接,僅是佔位符(可作錨點目標 name/id)。

支持的 URL 類型

類型示例說明
絕對 URLhttps://example.com/path完整協議+域名+路徑
相對 URL/path/page.html, ../sibling.html相對當前頁/域名根目錄
片段標識符#section1, /page.html#top頁內錨點跳轉
伪協議mailto:user@ex.com, tel:+8613800000000, sms:調用本地應用
JavaScriptjavascript:void(0), javascript:doSomething()強烈不推薦,阻礙無障礙/SEO/CSP
Data URIdata:text/html,<h1>Hi</h1>內嵌小資源
Blob/Filesystemblob:https://example.com/uuid臨時對象 URL

關鍵輔助屬性

屬性值/說明作用
target_blank (新標籤), _self (默認), _parent, _top, iframe名打開上下文
relnoopener (斬斷 opener), noreferrer (不發 Referer), nofollow (SEO 不傳權重), external, ugc, sponsored安全/SEO 關係聲明
downloadfilename.ext強制下載而非導航
hreflangzh-Hant, en目標語言提示
typetext/html, application/pdfMIME 類型提示
pinghttps://analytics.com/click點擊上報(空格分隔多 URL)
referrerpolicyno-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=..." 確保爬蟲可發現。
copyright © 萬盛學電腦網 all rights reserved