Map areas 集合

定義和用法

area 集合返回圖像映射中所有 <area> 元素 的集合。

注釋:集合中的元素按照它們在源代碼中出現的順序進行排序。

提示:如需返回所有已規定 href 屬性的 <area> 元素 的集合,請使用 links 集合

實例

例子 1

查明指定的圖像映射中有多少 <area> 元素:

var x = document.getElementById("planetmap").areas.length;

親自試一試

x 的結果是:

3

頁面下方有更多 TIY 實例。

語法

mapObject.areas

屬性

描述
length

返回集合中 <area> 元素的數量。

注釋:此屬性是只讀的

方法

方法 描述
[index]

返回集合中有指定索引的 <area> 元素(從 0 開始)。

注釋:如果索引號超出范圍,則返回 null。

item(index)

返回集合中有指定索引的 <area> 元素(從 0 開始)。

注釋:如果索引號超出范圍,則返回 null。

namedItem(id)

返回集合中有指定 id 的 <area> 元素。

注釋:如果 id 不存在則返回 null。

技術細節

DOM 版本: Core Level 2 Document Object
返回值:

HTMLCollection 對象,表示文檔中圖像映射中的所有 <area> 元素。

集合中的元素按照它們在源代碼中出現的順序進行排序。

瀏覽器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持

更多實例

例子 2:[index]

獲取圖像映射中第一個 <area> 元素的 URL:

var x = document.getElementById("planetmap").areas[0].href;

親自試一試

x 的結果將是:

https://www.codew3c.com/jsref/sun.html

例子 3:item(index)

獲取圖像映射中第一個 <area> 元素的 URL:

var x = document.getElementById("planetmap").areas.item(0).href;

親自試一試

x 的結果將是:

https://www.codew3c.com/jsref/sun.html

例子 4:namedItem(id)

獲取圖像映射中 id="myArea" 的 <area> 元素的 URL:

var x = document.getElementById("planetmap").areas.namedItem("myArea").href;

親自試一試

x 的結果將是:

https://www.codew3c.com/jsref/mercur.html

例子 5

遍歷圖像映射中的所有 <area> 元素并輸出每個區域的形狀:

var x = document.getElementById("planetmap");
var txt = "";
var i;
for (i = 0; i < x.areas.length; i++) {
  txt = txt + x.areas[i].shape + "<br>";
}

親自試一試

x 的結果將是:

rect
circle
circle