我正在尝试使用jQuery快速轻松地删除页面上的重复图像。
我有一系列图片被拉入,大多数都是重复的或具有相同的网址。
我在这里尝试了解决方案:Filtering duplicate img src with jQuery unique or remove,但它似乎无法运作。
以前有人做过类似的事吗?
PHP:
public function getWeeklyTrackChartGrid($methodVars) {
// Check for required variables
if ( !empty($methodVars['group']) ) {
$vars = array(
'method' => 'group.getweeklytrackchart',
'api_key' => $this->auth->apiKey
);
$vars = array_merge($vars, $methodVars);
if ( $call = $this->apiGetCall($vars) ) {
$i = 0;
$loopN = $call->weeklytrackchart->track;
foreach ($loopN as $track ) {
require 'config.php';
//if(++$i > $userLimit*2) break;
$albumArtS = $tracks['album']['image']['small'] = (string) $track->image[0];
$albumArtM = $tracks['album']['image']['small'] = (string) $track->image[1];
$albumArtL = $tracks['album']['image']['small'] = (string) $track->image[2];
$playCounts = $tracks[$i]['playcount'] = (string) $track->playcount;
?>
<?php if ($playCounts > 1) { ?>
<?php if ($albumArtL) { ?>
<img src="<?php echo $albumArtL; ?>" />
<?php } ?>
<?php } else { ?>
<?php if ($albumArtM) { ?>
<img src="<?php echo $albumArtM; ?>" />
<?php } ?>
<?php } ?>
<?php if ($albumArtwork == "yes") { ?>
<?php if ($albumArt) { ?>
<?php } }?>
<?php $i++;
}
return $tracks;
}
else {
return FALSE;
}
}
else {
// Give a 91 error if incorrect variables are used
$this->handleError(91, 'You must include a group variable in the call for this method');
return FALSE;
}
}
jQuery的:
$(function(){
var srcs = [],
temp;
$("img").filter(function(){
temp = $(this).attr("src");
if($.inArray(temp, srcs) < 0){
srcs.push(temp);
return false;
}
return true;
}).remove();
});
答案 0 :(得分:1)
这个解决方案对我有用:
var img_array = $("#singleTracks img").map(function() {
return $(this).attr("src");
});
img_array = $.unique(img_array);
img_array.each(function(index, value){
var sel = '#singleTracks img[src="' + value+ '"]';
var imgs = $(sel).not($(sel + ':first'));
imgs.remove();
});