---
title: NamedNodeMap
slug: Web/API/NamedNodeMap
translation_of: Web/API/NamedNodeMap
---
<div>{{APIRef("DOM")}}</div>

<p><code><strong>NamedNodeMap</strong></code> 介面表示了 {{domxref("Attr")}} 物件的集合。雖然 <code>NamedNodeMap</code> 與 {{domxref("NodeList")}} 都能如陣列一般透過索引訪問成員,但和 <code>NodeList</code> 不同的是,<code>NamedNodeMap</code> 中的成員並沒有順序。</p>

<p><code>NamedNodeMap</code> 物件具有<em>即時性(live)</em>,如果其內部成員(屬性節點物件)發生改變,<code>NamedNodeMap</code> 物件會自動更新至最新的狀態。</p>

<div class="note">
<p>僅管被稱作 <code>NamedNodeMap</code>,但本介面並不是直接用來處理節點物件({{domxref("Node")}}),而是專門負責屬性節點物件({{domxref("Attr")}})。屬性節點是一種特殊的節點,在部分瀏覽器實作中依然存在。</p>
</div>

<h2 id="屬性">屬性</h2>

<p><em>This interface doesn't inherit any property.</em></p>

<dl>
 <dt>{{domxref("NamedNodeMap.length")}} {{ReadOnlyInline}}</dt>
 <dd>Returns the amount of objects in the map.</dd>
</dl>

<h2 id="方法">方法</h2>

<p><em>This interface doesn't inherit any method.</em></p>

<dl>
 <dt>{{domxref("NamedNodeMap.getNamedItem()")}}</dt>
 <dd>Returns a {{domxref("Attr")}}, corresponding to the given name.</dd>
 <dt>{{domxref("NamedNodeMap.setNamedItem()")}}</dt>
 <dd>Replaces, or adds, the {{domxref("Attr")}} identified in the map by the given name.</dd>
 <dt>{{domxref("NamedNodeMap.removeNamedItem()")}}</dt>
 <dd>Removes the {{domxref("Attr")}} identified by the given map.</dd>
 <dt>{{domxref("NamedNodeMap.item()")}}</dt>
 <dd>Returns the {{domxref("Attr")}} at the given index, or <code>null</code> if the index is higher or equal to the number of nodes.</dd>
 <dt>{{domxref("NamedNodeMap.getNamedItemNS()")}}</dt>
 <dd>Returns a {{domxref("Attr")}} identified by a namespace and related local name.</dd>
 <dt>{{domxref("NamedNodeMap.setNamedItemNS()")}}</dt>
 <dd>Replaces, or adds, the {{domxref("Attr")}} identified in the map by the given namespace and related local name.</dd>
 <dt>{{domxref("NamedNodeMap.removeNamedItemNS()")}}</dt>
 <dd>Removes the {{domxref("Attr")}} identified by the given namespace and related local name.</dd>
</dl>

<h2 id="規範">規範</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#interface-namednodemap', 'NamedNodeMap')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Deals with {{domxref("Attr")}} instead of {{domxref("Node")}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM3 Core', 'core.html#ID-1780488922', 'NamedNodeMap')}}</td>
   <td>{{Spec2('DOM3 Core')}}</td>
   <td>No change from {{SpecName('DOM2 Core')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Core', 'core.html#ID-1780488922', 'NamedNodeMap')}}</td>
   <td>{{Spec2('DOM2 Core')}}</td>
   <td>Added <code>getNamedItemNS()</code>, <code>setNamedItemNS()</code> and <code>removeNamedItemNS()</code></td>
  </tr>
  <tr>
   <td>{{SpecName('DOM1', 'core.html#ID-1780488922', 'NamedNodeMap')}}</td>
   <td>{{Spec2('DOM1')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Deals with {{domxref("Attr")}} rather than {{domxref("Node")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop(22)}}<sup>[1]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Edge</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Deals with {{domxref("Attr")}} rather than {{domxref("Node")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile(22)}}<sup>[1]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] In Gecko 22 this interface was named <code>mozNamedAttrMap</code>. In Gecko 34 it was named back to <code>NamedNodeMap</code>.</p>

<h2 id="參見">參見</h2>

<ul>
 <li>{{domxref("Element.attributes")}}</li>
</ul>