超鏈接是 Web 的靈魂。設計優秀的鏈接體驗,需兼顧**視覺辨識度、交互反饋、打開行為、無障礙訪問、SEO 權重傳遞**。本文從表現形式分類、打開方式決策、代碼實現模式、無障礙屬性四個維度,給出生產級設計規範。
根據業務重要性與上下文,將鏈接分為三類,給予差異化視覺處理:
| 類型 | 典型場景 | 視覺規範 | 交互狀態 |
|---|---|---|---|
| 主要行動鏈接 (Primary Action) | 文章標題、核心 CTA、產品名稱、主要導航項 | 品牌色/高對比度色 + 下劃線(hover) 或 背景色塊按鈕樣式 | :hover 變色/加粗/陰影、:focus 明顯外框、:active 下沉 |
| 次要/提示性鏈接 (Secondary) | 作者名、標籤、「更多」、段落內參考鏈接、面包屑 | 次要色/弱化色 + **下劃線**(關鍵:與正文區分) | :hover 變主色/加深、:focus 外框 |
| 功能性/導航鏈接 (Functional/Nav) | Header 導航、分頁、Tab、Dropdown 觸發、按鈕式鏈接 | 按鈕/標籤/選單外觀,**不依賴顏色/下劃線**識別 | 狀態樣式完整:default/hover/active/focus/disabled |
a { color: inherit; text-decoration: none; } /* 由組件決定樣式 */
a:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; } /* 鍵盤聚焦可見 */
| 場景 | 推薦方式 | 代碼 | 理由 |
|---|---|---|---|
| 站內導航、分頁、Tab、功能操作 | **當前頁跳轉** | `<a href="/page">` | 保持歷史棧乾淨、支持後退、無彈窗干擾 |
| 列表頁項目(文章、商品、詳情) | **新標籤頁** | `<a href="..." target="_blank" rel="noopener noreferrer">` | 用戶對比多項時保留列表上下文 |
| 外部網站、友情鏈接、第三方文檔 | **新標籤頁** | 同上 + `rel="external nofollow noopener noreferrer"` | 不傳遞權重、安全隔離、保留本站 |
| 文件下載 (PDF、ZIP、圖片) | **觸發下載** | `<a href="file.pdf" download="建議檔名.pdf">` | 直接下載而非瀏覽器預覽 |
| 錨點跳轉(頁面內) | **平滑滾動** | `<a href="#section-id">` + CSS `scroll-behavior: smooth` | 保留上下文、無歷史棧污染 |
<!-- 1. 可訪問名稱:鏈接文本必須清晰描述目標 -->
<a href="/pricing">查看定價方案</a> <!-- 好 -->
<a href="/pricing">點擊這裡</a> <!-- 差:無語義 -->
<a href="/pricing"><img src="icon.png" alt="查看定價"></a> <!-- 圖片鏈接必須有 alt -->
<!-- 2. 鍵盤可達:Tab 可聚焦、Enter 執行 -->
<!-- 3. 焦點可見::focus-visible 樣式明顯 -->
<!-- 4. ARIA 輔助(複雜組件) -->
<a href="#" role="button" aria-expanded="false" aria-controls="menu">菜單</a>
<!-- Link.vue / Link.tsx 通用組件 -->
<script setup>
defineProps({
href: String,
target: { type: String, default: '_self' },
rel: String,
variant: { type: String, default: 'primary' }, // primary | secondary | nav
download: String,
to: String, // Vue Router 支持
})
const computedRel = computed(() => {
const rels = []
if (props.target === '_blank') rels.push('noopener', 'noreferrer')
if (props.rel) rels.push(props.rel)
return rels.join(' ') || undefined
})
</script>
<template>
<a
:href="to || href"
:target="target"
:rel="computedRel"
:download="download"
:class="['link', `link--${variant}`]"
>
<slot></slot>
</a>
</template>
<style>
.link { text-decoration: none; font-weight: 500; transition: color .2s; }
.link:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
.link--primary { color: var(--color-brand); }
.link--primary:hover { text-decoration: underline; }
.link--secondary { color: var(--color-text-muted); text-decoration: underline; }
.link--secondary:hover { color: var(--color-brand); }
.link--nav { color: var(--color-text); padding: .5rem 1rem; border-radius: 4px; }
.link--nav:hover { background: var(--color-bg-hover); }
</style>
超鏈接看似簡單,實則承載著**導航、轉化、信任傳遞、無障礙、SEO** 多重使命。建立設計系統級的鏈接組件、統一 `target/rel` 策略、堅持語義化錨文本,是專業前端團隊的基本功。