统计页面的标签种类,并排序
javascript
function countHtmlTags(tags) {
const tagCounts = {};
function dsf(obj) {
if (!obj || typeof obj !== 'object') return;
// 获取当前html标签的名称
const key = (obj.tagName || 'unknown').toLowerCase();
tagCounts[key] = (tagCounts[key] || 0) + 1;
for (let child of obj.children) {
dsf(child)
}
}
dsf(tags);
// 将结果转换为数组并排序
const sortedTags = Object.entries(tagCounts).sort((a, b) => b[1] - a[1]);
return sortedTags;
}
// 测试
console.log(countHtmlTags(document.body))