tagName和nodeName的区别
2014-08-30 14:30
423 查看
首先介绍DOM里常见的三种节点类型(总共有12种,如docment):元素节点,属性节点以及文本节点,例如<h2 class="title">head</h2>,其中h2是元素节点,class是属性节点,head是文本节点,在这里你可以说 h2这个元素节点包含一个属性节点和一个文本节点。其实几乎所有HTML的标签都是元素节点,而id, title, class等则是属性节点,而元素所包含的文本内容则是文本节点。
tagName和nodeName的语义是一样的,都是返回所包含标签的名称,例如上面的h2标签,都是返回h2,但是tagName只能在元素标签上使用,而nodeName则可以在所有的节点上使用。下面是nodeName在不同节点上的值:
而tagName只有在元素节点上才会有值。
从DOM层次来看,nodeName是node 接口上的property,而tagName是element 接口上的property,所有的节点(元素节点,属性节点,文本节点等12种)都继承了node接口,而只有元素节点才继承了element节点,因此nodeName比tagName具有更大的使用范围。
总结:tagName只能用在元素节点上,而nodeName可以用在任何节点上,可以说nodeName涵盖了tagName,并且具有更多的功能,因此建议总是使用nodeName。
tagName和nodeName的语义是一样的,都是返回所包含标签的名称,例如上面的h2标签,都是返回h2,但是tagName只能在元素标签上使用,而nodeName则可以在所有的节点上使用。下面是nodeName在不同节点上的值:
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 |
从DOM层次来看,nodeName是node 接口上的property,而tagName是element 接口上的property,所有的节点(元素节点,属性节点,文本节点等12种)都继承了node接口,而只有元素节点才继承了element节点,因此nodeName比tagName具有更大的使用范围。
总结:tagName只能用在元素节点上,而nodeName可以用在任何节点上,可以说nodeName涵盖了tagName,并且具有更多的功能,因此建议总是使用nodeName。
相关文章推荐
- Form,tagName和nodeName的区别
- tagName和nodeName的区别
- nodeName与tagName的区别
- javascript 中 tagName 和 nodeName 的区别
- 黄聪:Delphi 中的 XMLDocument 类详解(19) - NodeValue 与 NodeName 的读写区别
- 简单css选择器实现--支持#id、.className、@name、tagName.className、node节点五种格式
- 【HTML DOM】认识DOM的三大节点:元素节点,文本节点,属性节点以及nodeName,nodeType,nodeValue的区别
- node 中,url.parse(req.url).pathname 和 req.url 的区别
- tagName nodeName
- Delphi 中的 XMLDocument 类详解(19) - NodeValue 与 NodeName 的读写区别
- JavaScript DOM杂知识(value/innerHTML/innerText/textContent对比、nodeType/nodeName/nodeValue区别、获取任意类型的属性)
- DOM节点中属性nodeName、nodeType跟nodeValue的区别
- nodeName和tagName
- 浅谈nodeName,nodeValue,nodeType,typeof 的区别
- getElementById getElementByName getElementTagName的区别
- 认识DOM的三大节点:元素节点,文本节点,属性节点以及nodeName,nodeType,nodeValue的区别
- C++ builder 中的 XMLDocument 类详解(18) NodeValue 与 NodeName 的读写区别
- nodeName,nodeValue,nodeType,typeof 的区别
- 认识DOM的三大节点:元素节点,文本节点,属性节点以及nodeName,nodeType,nodeValue的区别
- 认识DOM的三大节点:元素节点,文本节点,属性节点以及nodeName,nodeType,nodeValue的区别