[英]How to remove div with a particular text in it
I have some div tags which has some text & elements in it & I want to remove those div's, They are looks like this 我有一些div标签,其中有一些文本和元素,我想删除那些div,它们看起来像这样
<div style="font-family:verdana;font-size:12px;">
Example
<a href="http://www.example.com" title="hey">example</a>
</div>
There are many div's like this & I want to remove them all with using jQuery or javascript 有很多这样的div我想用jQuery或javascript删除它们
If the elements have nothing in common such as a class, you can remove it by using the :contains
and remove()
method. 如果元素没有任何共同之处,例如类,则可以使用
:contains
和remove()
方法将其remove()
。
$("div:contains('Example')").remove()
Full example shown below: 完整示例如下所示:
$("div:contains('Example')").remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <div> Example </div> <div> Darren </div>
If the elements do have something in common you could use the class selector. 如果元素确实有共同点,则可以使用类选择器。
$(".common-class").remove();
Based on Darren's answer, if you want to be extra sure (as :contains
will match and delete any div containing the word example
), you can make sure it's a div that has an anchor with that same example as children, then go back to the parent and remove it. 基于Darren的回答,如果你想要更加确定(因为
:contains
将匹配并删除任何包含单词example
div),你可以确保它是一个div,它有一个与孩子相同的例子的锚,然后回到父母并删除它。
If this doesn't work, please paste a few more divs so we can see a common pattern and target it the safest way possible. 如果这不起作用,请粘贴几个div,这样我们就可以看到一个共同的模式,并以最安全的方式定位它。
$(document).ready(function(){ $('#remove').click(function(e){ $("div:contains('Example')").children("a:contains('example')").parent("div:contains('Example')").remove() }) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="font-family:verdana;font-size:12px;">Example<a href="http://www.example.com" title="hey"> example</a></div> <div style="font-family:verdana;font-size:12px;">Don't remove<a href="http://www.example.com" title="hey"> example</a></div> <div style="font-family:verdana;font-size:12px;">Example<a href="http://www.example.com" title="hey"> don't remove</a></div> <button id="remove"> Remove undesired divs </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.