首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

tagName与nodeName差异

2012-10-30 
tagName与nodeName区别两者都是取得HTML元素的类型:具体区别请看英文原文(本人翻译水平不好)。tagName and

tagName与nodeName区别
两者都是取得HTML元素的类型:

具体区别请看英文原文(本人翻译水平不好)。

tagName and nodeName are both useful Javascript properties for checking the name of an html element. For most purposes, either will do fine but nodeName is preferred if you are supporting only A-grade browsers and tagName is preferred if you intend to support IE5.5 as well.There are two issues with tagName:    * In all versions of IE, tagName returns ! when called on a comment node    * For text nodes, tagName returns undefined where as nodeName returns #textnodeName has it’s own set of issues but they are less severe:    * IE 5.5 returns ! when called on a comment node. This is less harmful than tagName which suffers from this behaviour across all versions of IE    * IE 5.5 doesn’t support nodeName for the document element or for attributes. Neither of these should be a concern for most practical purposes but should be kept in mind in any case    * Konqueror ignores comment nodes when using this property. But then again, Konqueror, along with IE 5.5 is not an A-grade browserSo for most practical purposes stick to nodeName due to its support for a wider range of scenarios and potentially better forward compatibility. Not to mention that it doesn’t hiccup on a comment node, which has a tendency to creep into code unannounced. Don’t worry about IE 5.5 or Konqueror as their market share is near 0%.

参考:http://ghsky.com/2010/08/tagname-vs-nodename.html。

区别是tagName对于早期IE5.0浏览器支持比较好nodeName在这点上支持不是很好,但是现在大多数用户是IE6以上了,所以这点可以放弃兼容性,而使用nodeName。

其实从名字上也好理解, tagName只对应html标签, nodeName对应着html节点对象的名称(key).
2 楼 deng131 2010-09-29   agName只对应html标签, nodeName对应着html节点对象的名称(key). 还是从语义上去理解。nodeName包含范围tagName,nodeName也可以表达html标签名(等同于tagName)。 3 楼 deng131 2010-09-29   document.getElementsByTagName('div')[0].attributes       //对象上的所有属性集合
document.getElementsByTagName('div')[0].getAttribute('') //取得对象上的指点属性
document.getElementById(aid).tagName; //返回html标签名
document.getElementsByTagName('div')[0].attributes[0].nodeName//对象节点名

热点排行