首先介绍DOM里常见的三种节点类型(总共有12种,如docment):元素节点,属性节点以及文本节点,例如<h2 class="title">head</h2>,其中h2是元素节点,class是属性节点,head是文本节点,在这里你可以说h2这个元素节点包含一个属性节点和一个文本节点。其实几乎所有HTML的标签都是元素节点,而id, title, class等则是属性节点,而元素所包含的文本内容则是文本节点。

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

而tagName只有在元素节点上才会有值。

从DOM层次来看,nodeName是node 接口上的property,而tagName是element 接口上的property,所有的节点(元素节点,属性节点,文本节点等12种)都继承了node接口,而只有元素节点才继承了element节点,因此nodeName比tagName具有更大的使用范围。

总结:tagName只能用在元素节点上,而nodeName可以用在任何节点上,可以说nodeName涵盖了tagName,并且具有更多的功能,因此建议总是使用nodeName。

最新文章

  1. 使用markdown
  2. iOS 9.3真机适配-Could not find Developer Disk Image问题
  3. Leetcode2:Add Two Numbers@Python
  4. TopShelf框架创建Windows服务作为Remoting的宿主案例:
  5. OS 系统下安装MySql 配置MySql环境变量
  6. ListView优化-ViewHolder的优化备份
  7. 高频交易策略之Penny Jump[z]
  8. Asp.Net中动态页面转静态页面
  9. css 字间距离_css 字体字符间距设置
  10. ITQ迭代量化方法解析
  11. liunx下安装mysql没有初始密码的解决方法
  12. flush privileges是什么意思?
  13. 实习生的Django[1]
  14. C--全排列的实现(递归方法) 傻子也能看懂的
  15. 转Delphi中Memo显示行号列号
  16. elasticsearch系列(四)部署
  17. 转摘app-稳定性测试
  18. BCrypt加密算法
  19. [转]MingGW64 多个版本区别(silj, seh)
  20. Spark2.X集群运行模式

热门文章

  1. 计算机中的换行符、回车符、\n、\r、\n\r 怎么区分啊?
  2. Lua脚本语法说明(转):
  3. NCE2
  4. 【原创】Spring 注入方式
  5. FreeMarker标签使用
  6. js数组去重的方法(转)
  7. python里的splitlines具体解释
  8. FTP response 421 received. Server closed connection
  9. ant jmeter jenkins 实现自动化测试
  10. glusterfs分布式文件系统