使用jQuery唯一过滤重复的img src或删除

我尝试过这里讨论过的不同的解决方法,但是没有一个在我的案例中工作。

这是我的来源:








我想只显示上面列表中唯一的“img src”,没有重复的图片……

脚本:

 var uniqueImg = $(".colourDots img").get(); divs = $.unique(uniqueImg); 

我又得到了相同的来源:-(

任何想法或任何其他解决方法?

试试这个:

 $(function(){ var srcs = [], temp; $(".colourDots img").filter(function(){ temp = $(this).attr("src"); if($.inArray(temp, srcs) < 0){ srcs.push(temp); return false; } return true; }).remove(); }); 

这是链接http://jsfiddle.net/xCjjp/

我要做的是,首先将所有那些图像标签的src attr放入一个数组中

 var img_array = $(".colourDots img").map(function() { return $(this).attr("src"); }); 

现在我可以在这个数组中做重复的检查过程,并删除所有在这个数组中多次找到src的img,这里所有img都被删除,所以我将添加一个src的单个图像。

你可以使用:

 $(".colourDots img").​​​​​​​​each(function(){ if($(this).parent().length) $(".colourDots img:contains('" + $(this).jpg() + "')").not(this).remove(); });​ 

见http://www.jquery4u.com/jquery-functions/jquery-each-examples/

这是我从所有答案中汇总的另一个版本:

 var img_array = $(".colourDots img").map(function() { return $(this).attr("src"); }); img_array = $.unique(img_array); img_array.each(function(index, value){ var sel = '.colourDots img[src="' + value+ '"]'; $(sel).not($(sel + ':first')).remove(); }); 

http://jsfiddle.net/GZWY5/3/