Node.nodeName - Web APIs 编辑
The nodeName
read-only property returns the name of the current Node
as a string.
Syntax
var str = node.nodeName;
Value
A DOMString
. Values for the different types of nodes are:
Interface | nodeName value |
---|---|
Attr | The value of Attr.name |
CDATASection | "#cdata-section" |
Comment | "#comment" |
Document | "#document" |
DocumentFragment | "#document-fragment" |
DocumentType | The value of DocumentType.name |
Element | The value of Element.tagName |
Entity | The entity name |
EntityReference | The name of entity reference |
Notation | The notation name |
ProcessingInstruction | The value of ProcessingInstruction.target |
Text | "#text" |
Example
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 the 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
will return "#text"
for text nodes while tagName
will return undefined
.
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'nodeName' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论