--- title: Node.nodeName slug: Web/API/Node/nodeName translation_of: Web/API/Node/nodeName --- <div> <div>{{APIRef("DOM")}}</div> </div> <p><code><strong>Node.nodeName</strong></code> 唯讀屬性會回傳目前節點名稱的字串。</p> <p>不同類型節點之回傳值:</p> <table class="standard-table"> <tbody> <tr> <th>介面</th> <th>nodeName 值</th> </tr> <tr> <td>{{domxref("Attr")}}</td> <td>The value of {{domxref("Attr.name")}}</td> </tr> <tr> <td>{{domxref("CDATASection")}}</td> <td><code>"#cdata-section"</code></td> </tr> <tr> <td>{{domxref("Comment")}}</td> <td><code>"#comment"</code></td> </tr> <tr> <td>{{domxref("Document")}}</td> <td><code>"#document"</code></td> </tr> <tr> <td>{{domxref("DocumentFragment")}}</td> <td><code>"#document-fragment"</code></td> </tr> <tr> <td>{{domxref("DocumentType")}}</td> <td>The value of {{domxref("DocumentType.name")}}</td> </tr> <tr> <td>{{domxref("Element")}}</td> <td>The value of {{domxref("Element.tagName")}}</td> </tr> <tr> <td>{{domxref("Entity")}}</td> <td>The entity name</td> </tr> <tr> <td>{{domxref("EntityReference")}}</td> <td>The name of entity reference</td> </tr> <tr> <td>{{domxref("Notation")}}</td> <td>The notation name</td> </tr> <tr> <td>{{domxref("ProcessingInstruction")}}</td> <td>The value of {{domxref("ProcessingInstruction.target")}}</td> </tr> <tr> <td>{{domxref("Text")}}</td> <td><code>"#text"</code></td> </tr> </tbody> </table> <h2 id="語法">語法</h2> <pre class="syntaxbox">var <em>str</em> = <em>node</em>.nodeName; </pre> <h2 id="範例">範例</h2> <p>Given the following markup:</p> <pre class="brush:html"><div id="d1">hello world</div> <input type="text" id="t"/> </pre> <p>and the following script:</p> <pre class="brush:js">var div1 = document.getElementById("d1"); var text_field = document.getElementById("t"); text_field.value = div1.nodeName; </pre> <p>In XHTML (or any other XML format), <code>text_field</code>'s value would read "div". However, in HTML, <code>text_field</code>'s value would read "DIV", because <code>nodeName</code> and <code>tagName</code> return in upper case on HTML elements in DOMs flagged as HTML documents. Read more <a href="http://ejohn.org/blog/nodename-case-sensitivity/" title="http://ejohn.org/blog/nodename-case-sensitivity/">details on nodeName case sensitivity in different browsers</a>.</p> <p>Note that <code><a href="/en-US/docs/DOM/element.tagName" title="DOM/element.tagName">tagName</a></code> property could have been used instead, since <code>nodeName</code> has the same value as <code>tagName</code> for an element. Bear in mind, however, that <code>nodeName</code> will return <code>#text</code> for text nodes while <code>tagName</code> will return <code>undefined</code>.</p> <h2 id="規範">規範</h2> <ul> <li><a href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68D095">DOM Level 2 Core: Node.nodeName</a></li> <li><a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-F68D095">DOM Level 3 Core: Node.nodeName</a></li> <li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#apis-in-html-documents">HTML 5: APIs in HTML documents</a></li> </ul>