[英]How do you set only the content of the grand-grand-…-children of a div to ''?
You have a html like this 你有一个这样的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>
How do you set only the content of the "nestedmost" div to '' in a userscript that works with most pages? 如何在适用于大多数页面的用户脚本中仅将“最嵌套的” div的内容设置为“”? In this context,
How are you
stays but <p>Hello</p>
is removed. 在这种情况下,
How are you
但<p>Hello</p>
被删除了。
Context: the goal is to automatically filter forum posts(ie the smallest unit) or articles that contain certain keywords but not the whole page because somewhere nested there are keywords. 上下文:目标是自动过滤论坛帖子(即最小的单元)或包含某些关键字但不包含整个页面的文章,因为嵌套的某处有关键字。
It's not entirely clear to me what you are looking for, but here's a function that function that finds the most deep div
in the document body tree: 对我来说,您要查找的内容还不是很清楚,但是下面的函数可以在文档正文树中找到最深的
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;
}
You can use it to delete the contents of the deepest div like so: 您可以使用它删除最深的div的内容,如下所示:
var deepest_div = find_deepest_div();
if (deepest_div) deepest_div.innerHTML = "";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.