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 #text
nodeName 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 browser
So 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。

你可能感兴趣的:(JavaScript,html,浏览器,IE)