nodeName read-only property returns the name of the current 节点 以字符串形式。

句法

var str = node.nodeName;
					

A DOMString . Values for the different types of nodes are:

接口 nodeName value
Attr Attr.name
CDATASection "#cdata-section"
注释 "#comment"
Document "#document"
DocumentFragment "#document-fragment"
DocumentType DocumentType.name
元素 Element.tagName
Entity The entity name
EntityReference The name of entity reference
Notation The notation name
ProcessingInstruction ProcessingInstruction.target
文本 "#text"

范例

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" ,因为 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 .

注意, Element.tagName property could have been used instead, since nodeName has the same value as tagName for an element. Bear in mind, however, that nodeName 将返回 "#text" for text nodes while tagName 将返回 undefined .

规范

规范 状态 注释
DOM
The definition of 'nodeName' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
nodeName Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

元数据

  • 最后修改: