假设我的代码中有这样的结构:
因此,我只会得到这个:
我尝试了这种方法:
<div>
<div>This is my text</div>
<div>Text continues...</div>
</div>
<div>
<div>Another text</div>
<div>Other text</div>
</div>
<div>
<div>This is my text</div>
<div>Text continues...</div>
</div>
我想要做的事情是(不使用 jquery):
搜索包含文本部分 this is my
的 div(注意,我没有传递完整的文本,只是部分文本 this is my
)。
每当我找到包含这部分文本的 div 时,我都想要删除父 div!因此,删除其中的所有内容,包括:
<div>This is my text</div>
and
<div>Other text</div>
因此,我只会得到这个:
<div>
<div>Another text</div>
<div>Other text</div>
</div>
我尝试了这种方法:
var elems = document.querySelectorAll("div"),
res = Array.from(elems).find(v => v.textContent == 'Vídeos que contêm');
alert(res ? 'found!' : 'not found');
但它只能搜索特定的文本!而且它仍然无法适用于每一个情况。