How would I generate a list of distinct HTML tags in a page using JavaScript (must work in IE6)

后端 未结 4 704
轮回少年
轮回少年 2021-01-28 10:53

The end result I\'m after is a JavaScript array containing a list of tag names that are used in the HTML document eg:

div, span, section, h1, h2, p, etc...

I wan

4条回答
  •  無奈伤痛
    2021-01-28 11:37

    To get a unique list of tagnames in a document as an array that works in all browsers back to IE 6 and equivalent:

    function listTagNames() {
      var el, els = document.body.getElementsByTagName('*');
      var tagCache = {};
      var tagname, tagnames = [];
    
      for (var i=0, iLen=els.length; i

    If you think there might be an inherited object property that is the same as a tag name, use a propertyIsEnumerable test:

          if (!tagCache.propertyIsEnumerable(tagname)) { 
    

    so it will work even in Safari < 2.

提交回复
热议问题