有没有人知道如何获取页面上存在的所有HTML标签? 我需要仅获取标签,而不带其ID或其他属性,并创建一种树形结构。 最好使用JavaScript或JQuery完成。
例如,以下是HTML代码:
<html>
<head>
<title>
Example Page
</title>
</head>
< body>
<h1 style="somestyle">
Blabla
</h1>
<div id="id">
<table id="formid">
<tr>
<td>
</td>
</tr>
</table>
</div>
</body>
</html>
应该返回return:
html
head
title
body
h1
div
table
tr
td
document.querySelectorAll('*')
或者递归遍历 DOM(document.body
)的.children
。 - A1rPun