The tagname and nodename have the same semantics and both return the names of the included tags. For example, the above H2 tags all return H2, but tagname can only be used on element tags, nodename can be used on all nodes. The values of nodename on different nodes are as follows:
Interface |
Nodename |
Nodevalue |
Attributes |
ATTR |
Name of attribute |
Value of attribute |
Null |
Cdatasection |
# CDATA-Section |
Content of the CDATA section |
Null |
Comment |
# Comment |
Content of the comment |
Null |
Document |
# Document |
Null |
Null |
Documentfragment |
# Document-Fragment |
Null |
Null |
Documenttype |
Document Type name |
Null |
Null |
Element |
Tag Name |
Null |
Namednodemap |
Entity |
Entity name |
Null |
Null |
Entityreference |
Name of entity referenced |
Null |
Null |
Notation |
Notation name |
Null |
Null |
Processinginstruction |
Target |
Entire content excluding the target |
Null |
Text |
# Text |
Content of the text node |
Null |
Tagname has a value only on the element node.
From the DOM level, nodename is the property on the node interface, while tagname is the property on the element interface. All nodes (12 types of element nodes, attribute nodes, and text nodes) only the Element Node inherits the element node. Therefore, nodename has a larger scope of use than tagname.
Conclusion: tagname can only be used on element nodes, while nodename can be used on any node. It can be said that nodename covers tagname and has more functions. Therefore, we recommend that you always use nodename.
Differences between tagname and nodename