jQuery删除具有类名的重复div [英] jQuery remove duplicated div with class name
本文介绍了jQuery删除具有类名的重复div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以基本上我的div生成如下:
So basically my div generates like this:
<div class="test className">content</div>
<div class="test className">content</div>
<div class="test className">content</div>
..
<div class="test className">content</div>
,我正在尝试删除重复的div,并仅保留最后一个!有什么好主意吗?谢谢!
and I'm trying to remove duplicated divs and keep last one only! any quick ideas? thanks!
推荐答案
基于您提供的标记的快速提示.
var $div = $('div.test.className:contains(content)');
if ($div.length > 1) {
$div.not(':last').remove()
}
但是我会避免重复.
编辑:这是使用filter
和slice
方法的另一种选择:
edit: Here is another alternative using filter
and slice
method:
$('.test.className').filter(function() {
return this.textContent === 'content';
}).slice(0, -1).remove();
在上面的片段中,使用-1
作为.slice
方法的第二个参数,该集合中的最后一个元素将被忽略.
In the above snippet by using -1
as the second argument of the .slice
method, the last element in the set is ignored.
这篇关于jQuery删除具有类名的重复div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文