2013-06-18 89 views
-1

我想找到一個快速簡單的方法去除頁面上的重複圖像與jQuery。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(); 
}); 
+1

您可以發佈您的代碼,請? –

+0

添加上面的代碼 – danyo

+2

該代碼與jQuery有什麼關係?那是PHP – Liam

回答

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(); 
});