Node.nodeName 唯讀屬性會回傳目前節點名稱的字串。


介面 nodeName 值
Attr (en-US) The value of (en-US)
CDATASection (en-US) "#cdata-section"
Comment (en-US) "#comment"
Document "#document"
DocumentFragment "#document-fragment"
DocumentType The value of (en-US)
Element The value of Element.tagName (en-US)
Entity The entity name
EntityReference The name of entity reference
Notation The notation name
ProcessingInstruction (en-US) The value of (en-US)
Text (en-US) "#text"


var str = node.nodeName;


Given the following markup:

<div id="d1">hello world</div>
<input type="text" id="t"/>

and the following script:

var div1 = document.getElementById("d1");
var text_field = document.getElementById("t");

text_field.value = div1.nodeName;

In XHTML (or any other XML format), text_field's value would read "div". However, in HTML, text_field's value would read "DIV", because nodeName and tagName return in upper case on HTML elements in DOMs flagged as HTML documents. Read more details on nodeName case sensitivity in different browsers.

Note that tagName (en-US) property could have been used instead, since nodeName has the same value as tagName for an element. Bear in mind, however, that nodeName will return #text for text nodes while tagName will return undefined.


DOM Standard
# ref-for-dom-node-nodename①