获取HTML页面有多少种标签

前端小测-JS 算法题目(数组相关):

注意是获取页面有多少种标签,不是多少个

已知如下 html 页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div>
<header>
获取HTML页面有多少种标签
<div>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
</header>
<section>
<span></span>
<p></p>
<p></p>
</section>
<footer>
<div>
<h1></h1>
<h2></h2>
</div>
</footer>
</div>
</body>
</html>

获取页面多少种标签方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
console.log("result", [...document.querySelectorAll("*")]);

console.log(new Set([...document.querySelectorAll("*")]));

const result = [...document.querySelectorAll("*")].map((item) => item.nodeName);

console.log("result", [...new Set(result)].length);

const findHtmlTagsName = () => {
return [
...new Set(
[...document.querySelectorAll("*")].map((item) => item.nodeName)
),
].length;
};

console.log("result length", findHtmlTagsName());

控制台打印测试