js获取网页当中所有标签里面的文本
发布网友
发布时间:2022-04-19 20:23
我来回答
共1个回答
热心网友
时间:2022-04-22 14:52
<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<title>recursion</title>
<style type="text/css">
</style>
<script type="text/javascript">
var array = [];
onload = function ()
{
var tags = document.body.getElementsByTagName ('*');
for ( var i = 0; i < tags.length; i++)
{
array.push (getText (tags[i]));
}
console.log (array);
}
var getText = function (dom)
{
var index = 0, html = dom.innerHTML;
while (dom.children.length && index < dom.children.length)
{
var chtml = dom.children[index].outerHTML;
html = dom.innerHTML.replace(chtml, '');
index++;
}
return "<font>" + html + "</font>";
}
</script>
</head>
<body>
<p>
ths is p<a href="">this is a</a>wyz
</p>
<i>this is i</i>
<b>this is b</b>
</body>
</html>