简体   繁体   English

如何使用javascript删除具有特殊文本的div的父级?

[英]How to remove parent of a div has special text using javascript?

Assuming I have this structure in my code: 假设我的代码中具有以下结构:

<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>

What I'm trying to do is (with no jquery ): 我想做的是(没有jquery ):

Search for this is my (realize that I'm not passing the full text. Just a part of it this is my ). 搜索this is my (意识到我没有传递全文。 this is my的一部分)。

And everytime I find a div that contains this part of text, I want to remove the parent div! 每当我找到包含这部分文本的div时,我都想删除父div! Consequently, by removing everything within it, which includes: 因此,通过删除其中的所有内容,其中包括:

<div>This is my text</div> 
and
<div>Other text</div>

So as a result, I would have only this: 因此,我只有这样:

<div>
  <div>Another text</div>
  <div>Other text</div>
</div>

I tried this way: 我这样尝试:

var elems = document.querySelectorAll("div"),
    res = Array.from(elems).find(v => v.textContent == 'Vídeos que contêm');
alert(res ? 'found!' : 'not found');

But it searches for specific text! 但是它会搜索特定的文本! And it still does not work for every occurrence. 而且它仍然不能每次都起作用。

Your code doesn't work because: 您的代码无效,因为:

  1. You trying to check if textContent is equal to ( == ) string but you need to check if text content includes string. 您试图检查textContent是否等于( == )字符串,但是您需要检查文本内容是否includes字符串。

  2. It just find elements and doesn't remove them. 它只是找到元素而不会删除它们。 You need to use .remove() to removing elements. 您需要使用.remove()删除元素。

document.querySelectorAll("div > div:first-child").forEach(ele => {
  ele.textContent.includes('This is my') && ele.parentNode.remove();
});

 document.querySelectorAll("div > div:first-child").forEach(ele => { ele.textContent.includes('This is my') && ele.parentNode.remove(); }); 
 <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> 


I'm trying to do with no jquery 我正在尝试没有jQuery

Although you don't want to use jQuery but it is very simple to do and has less code 尽管您不想使用jQuery,但是它非常简单并且代码更少

$("div > div:contains('This is my')").parent().remove();   

 $("div > div:contains('This is my')").parent().remove(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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> 

使用包含方法

res = Array.from(elems).find(v => v.textContent.contains('Vídeos que contêm'));

You can use filter and foreach combination to remove it. 您可以使用filterforeach组合将其删除。

Note: you can add toLowerCase check to make it more reliable. 注意:您可以添加toLowerCase检查以使其更可靠。

Array.from(elems).filter(v => v.textContent.indexOf('Another')!==-1).forEach(a=> a.remove());

 var elems = document.querySelectorAll("div"); Array.from(elems).filter(v => v.textContent.indexOf('Text continues')!==-1).forEach(a=> a.remove()); 
 <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> 

I have enhanced your code, but the find will return the first instance of the match found, hence the deleted node would only delete the first child. 我已经增强了您的代码,但是查找将返回找到的匹配项的第一个实例,因此,删除的节点只会删除第一个孩子。

You need to delete all the matched instances , hence need to loop over them 您需要删除所有匹配的实例,因此需要遍历它们

// This deletes all the matched instances rather than the first 
Array.from(elems)
  .filter(v => v.textContent.includes('This is my'))
  .map(matchedEle => matchedEle.remove());

Sample Snippet with your code and the modification : 示例代码片段以及您的代码和修改内容:

 var elems = document.querySelectorAll("div"), // This gets only the first instance. res = Array.from(elems).find(v => v.textContent.includes('This is my')); alert(res ? 'found!' : 'not found'); res.parentNode.removeChild(res); // This deletes all the matched instances rather than the first Array.from(elems) .filter(v => v.textContent.includes('This is my')) .map(matchedEle => matchedEle.remove()); 
 <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> 

You are doing it correctly but with a different text. 您的操作正确,但文字不同。 Instead of == matching the text inside of text content of the div just use String.includes() . 不用==匹配div文本内容内的文本,只需使用String.includes()

Just filter it this way to remove: 只需用这种方法过滤即可删除:

 var elems = document.querySelectorAll("div"); Array.from(elems).filter(v => v.textContent.includes('This is my') ? v.remove() : v); 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM