所以当它们的元素有这个时,我尝试删除一些 p html:pasdechier on。
我尝试使用这段代码,但它不起作用! 我准确地说 p 已经用 ajax 添加了。 我能怎么做 ?
$('.downloadfileexintegradedindb').removeByContent('pasdefichier');
$('.downloadfileexintegradedindb:contains("pasdefichier")').hide(); //to hide
$('.downloadfileexintegradedindb:contains').find('p:contains("pasdefichier")').remove();
html代码:
<p class="downloadfileexintegradedindb" id="123456789" style="position: absolute; top: 36px; left: 123px;">pasdefichier</p>
最佳答案
下面的片段是你想要实现的。
我使用这些函数使其工作:
.html()
获取元素的内容,.includes()
检查此内容中的字符串。
请注意,与 $('p:contains(text)')
相比,我更喜欢这种方法,因为我们可以使用它轻松添加 else
。
当你使用 jQuery 时,我也在我的代码片段中使用了它。
如果 .hide()
更符合您的需要,您当然可以使用 .remove()
。
- 如果只有一个元素:
if ($('#123456789').html().includes('pasdefichier')) {
console.log('Removed p!');
$('#123456789').remove();
} else {
// Do something else
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="downloadfileexintegradedindb" id="123456789" style="position: absolute; top: 36px; left: 123px;">pasdefichier</p>
- 如果您有多个元素:
$('.p-class').each(function(){
if($(this).html().includes('pasdefichier')){
$(this).remove();
} else {
// Do something else
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="p-class">pasdefichier</p>
<p class="p-class">fichier!</p>
<p class="p-class">fichier!</p>
<p class="p-class">pasdefichier</p>
希望对您有所帮助。
关于javascript - 删除包含特定字符串的 <p>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50544256/