你有像这样的HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>
</title>
</head>
<body>
<div>
<div>
How are you?
</div>
<div>
<div>
<div>
<p>
Hello
</p>
</div>
</div>
</div>
</div>
<div>
</div>
</body>
</html>
如何在与大多数页面一起使用的用户脚本中仅将“nestedmost”div的内容设置为“?”?在此上下文中,How are you
会停止,但会移除<p>Hello</p>
。
上下文:目标是自动过滤论坛帖子(即最小的单元)或包含特定关键字但不包含整个页面的文章,因为嵌套的地方有关键字。
答案 0 :(得分:1)
我并不完全清楚你在寻找什么,但这是一个功能,可以找到文档正文树中最深div
的函数:
function find_deepest_div() {
var deepest_div;
var deepest_div_depth = -1;
function search(current, depth) {
if (current.tagName === "DIV" && depth > deepest_div_depth) {
deepest_div = current;
deepest_div_depth = depth;
}
for (var i = 0, len = current.children.length; i < len; i++)
search(current.children[i], depth + 1);
}
search(document.body, 0);
return deepest_div;
}
您可以使用它来删除最深的div的内容,如下所示:
var deepest_div = find_deepest_div();
if (deepest_div) deepest_div.innerHTML = "";