萬盛學電腦網

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

HTML `` `` 標籤完整參數詳解與實戰代碼範例

<map> 定義客戶端圖像映射,配合 `` 在圖片上劃定可點擊熱點。本文詳細列出所有屬性、形狀座標規則、完整代碼範例與現代最佳實踐。

標籤關係與必要條件

  • `` **成對標籤**:`...`
  • `` **單標籤**:``(HTML5 可省略斜線)
  • `` 的值**必須與** `` 完全一致(含 `#`)。
  • `` 可放在 `` 任何位置,建議緊跟 `` 之後或頁尾。

`` 完整屬性表

屬性值類型說明
shape`rect` \| `circle` \| `poly` \| `default`熱點形狀,預設 `rect`
coords字串座標列表,格式見下表
hrefURL連結目標,缺失則無連結(可作純標記)
alt字串必填,替代文字(無障礙/SEO)
target`_blank` \| `_self` \| `_parent` \| `_top` \| framename連結開啟目標
nohref布林 (HTML4)明確表示無連結,**HTML5 廢棄**,直接省略 `href` 即可
tabindex整數Tab 順序
accesskey字符快捷鍵
rel字串連結關係(如 `noopener`, `nofollow`)
download字串/布林HTML5:提示下載而非導航
pingURL 列表點擊時發送 POST 到指定 URL(追蹤用)

形狀與座標詳細規則

shapecoords 格式座標含義範例
rect`x1,y1,x2,y2`左上 (x1,y1)、右下 (x2,y2)`10,20,110,80`
circle`x,y,r`圓心 (x,y)、半徑 r`200,150,30`
poly`x1,y1,x2,y2,...,xn,yn`頂點序列(至少 3 對),自動閉合`50,0 100,50 50,100 0,50` (菱形)
default(無)整張圖片其餘區域—

座標基於**原始圖片像素尺寸**,左上角為 (0,0)。

標準代碼範例

<img src="dashboard.png" usemap="#dashMap" alt="儀表板" width="800" height="600">

<map name="dashMap">
  <!-- 矩形:總覽按鈕 -->
  <area shape="rect" coords="20,20,180,80" href="/overview" alt="總覽" target="_blank">
  
  <!-- 圓形:通知鈴鐺 -->
  <area shape="circle" coords="750,50,25" href="/notifications" alt="通知">
  
  <!-- 多邊形:不規則側邊欄觸發區 -->
  <area shape="poly" coords="0,150, 50,150, 50,450, 0,450" href="/sidebar" alt="展開側邊欄">
  
  <!-- 兜底:其他區域點擊回首頁 -->
  <area shape="default" href="/" alt="返回首頁">
</map>

進階技巧

1. JavaScript 動態綁定事件(而非 href 導航)

<area shape="rect" coords="10,10,100,50" data-action="openModal" data-id="123" alt="編輯">

<script>
document.querySelector('map').addEventListener('click', e => {
  const area = e.target.closest('area');
  if (!area) return;
  e.preventDefault(); // 阻止導航
  const action = area.dataset.action;
  if (action === 'openModal') openModal(area.dataset.id);
});
</script>

2. 響應式座標自動縮放

function scaleMapAreas(mapName, displayedWidth, displayedHeight, naturalWidth, naturalHeight) {
  const scaleX = displayedWidth / naturalWidth;
  const scaleY = displayedHeight / naturalHeight;
  document.querySelectorAll(`map[name="${mapName}"] area[coords]`).forEach(area => {
    const coords = area.coords.split(',').map(Number);
    const scaled = coords.map((v, i) => Math.round(v * (i % 2 === 0 ? scaleX : scaleY)));
    area.coords = scaled.join(',');
  });
}

// 圖片載入後調用
img.onload = () => scaleMapAreas('dashMap', img.clientWidth, img.clientHeight, img.naturalWidth, img.naturalHeight);
window.addEventListener('resize', () => scaleMapAreas(...));

3. 無障礙增強

  • 每個 `` 加 `` 或 `aria-label` 提供懸浮提示。</li> <li>鍵盤可聚焦:確保 `tabindex` 邏輯順序合理。</li> <li>提供文字版同功能連結列表(`noscript` 或頁腳)。</li> </ul> <h2>現代替代:SVG 圖像映射</h2> <p>如前文所述,SVG 原生支援向量縮放、CSS 樣式、動畫、語義結構,**強烈建議新專案直接用 SVG `<a>` + 幾何圖形** 替代 `<map>`/`<area>`。</p> <h2>快速檢查清單</h2> <ul> <li>☐ `usemap="#name"` 與 `map name="name"` 完全一致</li> <li>☐ 每個 `<area>` 有 `alt` 屬性</li> <li>☐ 座標對應原始圖片尺寸(或已做響應式縮放)</li> <li>☐ 熱點區域不重疊(重疊以文檔順序優先)</li> <li>☐ 測試鍵盤導航與螢幕閱讀器</li> </ul> <p>掌握 `<map>`/`<area>` 細節,能在舊專案維護與特定場景(如郵件模板、舊系統嵌入)中游刃有餘。</p> <div class="ad2"> <script language='javascript' src='/ad/tad.js'></script> </div> </div> <div> <div class="pageNum"> <ul> <li>上一篇程序編程: <a class='LinkPrevArticle' href='https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35093.html' >HTML 客戶端圖像地圖教學:<map>、<area> 與 usemap 完整範例</a> <li>下一篇程序編程: <a class='LinkNextArticle' href='https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35095.html' >CSS `text-overflow:ellipsis` 實現單行/多行文字溢出省略號完整指南</a> </ul> </div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> <div class=hebxbne_left-div-2> <h2 style="text-align:left">HTML 腳本教學排行</h2> <ul> <li><a class="font7 nleft" title="HTML 表格寬度與高度屬性 WIDTH/HEIGHT 詳解" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35060.html">HTML 表格寬度與高度屬性 WIDTH/HEIGHT 詳解</a></li> <li><a class="font7 nleft" title="iframe 父子頁面 JS 互調完全手冊:從基礎到跨幀通信最佳實踐" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35176.html">iframe 父子頁面 JS 互調完全手冊:從基礎到跨幀通信最佳實踐</a></li> <li><a class="font7 nleft" title="DB2 pureXML 技術回顧:從關係型到原生 XML 儲存的範式轉移" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35035.html">DB2 pureXML 技術回顧:從關係型到原生 XML 儲存的範式轉移</a></li> <li><a class="font7 nleft" title="十六進位顏色代碼速查表:紅、藍、綠、灰階與 16 色安全色" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35112.html">十六進位顏色代碼速查表:紅、藍、綠、灰階與 16 色安全色</a></li> <li><a class="font7 nleft" title="使用 Meadroid ScriptX ActiveX 實現無提示直接列印" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35031.html">使用 Meadroid ScriptX ActiveX 實現無提示直接列印</a></li> </ul> </div> <div class=kenbxne_js></div> </div> <div class="nright hebxbne_right"> <div class=guanggao1 id=guanggao_r1></div> <div class=kenbxne_js></div> <div class=hebxbne_right-div1> <h2>推薦文章</h2> <div class=kenbxne_js></div> <ul class=hebxbne_right-list1> <li><a class="font7 nleft" title="HTML 基礎標籤與屬性快速入門指南" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35042.html">HTML 基礎標籤與屬性快速入門指南</a></li> <li><a class="font7 nleft" title="HTML fieldset 與 legend 標籤:表單分組語意化與樣式化指南" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35018.html">HTML fieldset 與 legend 標籤:表單分組語意化與樣式化指南</a></li> <li><a class="font7 nleft" title="iframe 父子頁面 JS 互調完全手冊:從基礎到跨幀通信最佳實踐" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35176.html">iframe 父子頁面 JS 互調完全手冊:從基礎到跨幀通信最佳實踐</a></li> <li><a class="font7 nleft" title="CSS `a` 標籤偽類 `:link` `:visited` `:hover`…" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35089.html">CSS `a` 標籤偽類 `:link` `:visited` `:hover`…</a></li> <li><a class="font7 nleft" title="HTTP GET vs POST 深度對比:原理、編碼、安全、亂碼根因與最佳實踐" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35183.html">HTTP GET vs POST 深度對比:原理、編碼、安全、亂碼根因與最佳實踐</a></li> <li><a class="font7 nleft" title="HTML 標籤優化佈局:拒絕 Divitis,語義化與 CSS 選擇器高效應用" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35210.html">HTML 標籤優化佈局:拒絕 Divitis,語義化與 CSS 選擇器高效應用</a></li> <li><a class="font7 nleft" title="HTTP 長連接模擬服務端推送:Comet 長輪詢與 iframe 流技術原理" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/34839.html">HTTP 長連接模擬服務端推送:Comet 長輪詢與 iframe 流技術原理</a></li> <li><a class="font7 nleft" title="HTML Frameset 框架布局屬性完全指南:從入門到棄用替代方案" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35131.html">HTML Frameset 框架布局屬性完全指南:從入門到棄用替代方案</a></li> <li><a class="font7 nleft" title="Dreamweaver CS3 Spry 框架:零程式碼將 XML 資料綁定顯示…" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/34977.html">Dreamweaver CS3 Spry 框架:零程式碼將 XML 資料綁定顯示…</a></li> <li><a class="font7 nleft" title="從 HTML 頁面提取純中文文本:VBScript 正則與 escape 編碼兩…" href="https://twcomputer.wsxdn.com/programming/webproduction/htmlscripttutorial/201510/35136.html">從 HTML 頁面提取純中文文本:VBScript 正則與 escape 編碼兩…</a></li> </ul> </div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> <div class=plnebxmj> <div class="plnebxmj-2 flink"> <h2><a class=bottom href="https://twcomputer.wsxdn.com/" target=_blank>萬盛學電腦網</a>| <a class=bottom hreflang="ja" href="https://jp.wsxdn.com/" target=_blank>日本語版</a> | <a class=bottom onclick="this.style.behavior='url(#default#homepage)';this.sethomepage('https://twcomputer.wsxdn.com');" href="#">設為首頁</a> | <a class=bottom href="javascript:window.external.addfavorite('https://twcomputer.wsxdn.com','萬盛學電腦網');">加入收藏</a></h2> </div> <div class=knemnnx></div> </div> <div class="ojwbxnks">copyright © <a title="萬盛學電腦網" href="https://twcomputer.wsxdn.com/" target=_blank>萬盛學電腦網</a> all rights reserved <br> </div> </div> </body> </html>