2
统计当前页面含有多少个标签

ES6

const domNames = Array.form(document.querySelectorAll('*')).map(v => v.tagName);
const result = new Set(domNames);
console.log(`当前页面存在的标签个数为: ${result.size}`);

JS

const dom = document.querySelectorAll('*');
const domNames = [];
Array.from(dom).forEach(v => {
  domNames.push(v.tagName);
});

const obj = {};
const newArr = [];
domNames.forEach(name => {
  if(!obj[name]) {
    newArr.push(name);
    obj[name] = true;
  }
});
console.log(newArr.length);

原生JS统计页面所有标签的种类

var all = document.querySelectorAll('*');
    var tags = [];
    all.forEach(item => {
      tags.push(item.tagName.toLocaleLowerCase());
    });

    // 存储标签的种类以及相应的个数
    var result = {};
    for(var i = 0; i < tags.length; i++) {
      if(!result[tags[i]]) {
        result[tags[i]] = 1;
      } else {
        result[tags[i]]++;
      }
    }
    
    console.log(res);
    console.log(Reflect.ownKeys(res).length);

木子
38 声望3 粉丝

加油呀!!!