Implement a piece of js code to output how many different html tags are used in the current page

A question about judging how many different html tags are used on the page.
Define an array to store the same elements and define an empty array to store each tag.

Hope that the boss will provide a good method


    let tags = document.getElementsByTagName('*');
    let tagsArr = [];
  const countTag =() => {
    
    
    for (let i = 0; i < tags.length; i++) {
    
    
      tagsArr.push((tags[i].tagName).toLowerCase());//toLowerCase() 方法用于把字符串转换为小写。
    }
    // console.log(tagsArr);
    //定义一个数组用于存放相同的元素
    let temp = [];
  //定义一个空数组用于存放每一个标签;
    let tag =[];
    //循环遍历把相同的标签从tagsArr去除,添加到tag
    for (let i = 0; i < tagsArr.length; i++) {
    
    
      for (let j = i+1; j < tagsArr.length+1; j++) {
    
    
        if (tagsArr[i] == tagsArr[j]) {
    
    
          temp.push(tagsArr[j]);
          tagsArr.splice(j,1);
          j--;
        }
        if (j == tagsArr.length -i) {
    
    
          temp.push(tagsArr[i]);
          tagsArr.splice(i,1);
          i--;
          tag.push(temp);
          temp = [];
        }
      }
    }
    return tag;
  }
  console.log(countTag());//输出存放标签的数组
  let num = countTag()
  console.log(num.length)//输出不同标签的种类数

Guess you like

Origin blog.csdn.net/pz1021/article/details/105390530