2015-09-06 92 views
1

我在从另一个站点拉出的页面上有8个缩略图。当找不到图像时,网站会发回标准的80x80(自然尺寸)图像。我需要检测该80x80图像,并用我自己的图像替换它。替换某个自然尺寸的所有图片

我猜这可以用jQuery完成,但老实说,没有线索如何实现它。

任何帮助将不胜感激。

固定:我想出了一个解决方案,但不知道它是否是最好的。它的工作,所以我现在就用它。

<script type="text/javascript"> 

jQuery(document).ready(function(){ 
    jQuery('img').each(function(index){ 
     var img = jQuery(this); 
     var newImg = new Image(); 

     newImg.src = img.attr('src'); 

     if(newImg.height == 80 && newImg.width == 80){ 
      jQuery(this).attr("src", "/2/files/no_image.jpg") 
     } 
    }); 
}); 

</script> 

感谢

史蒂夫

+0

你为什么不尝试它,然后显示代码,并寻求帮助? –

+0

因此我提出了问题 –

+0

为什么你不简单测试'img'的维度? ('newImg'的目的是什么...) – Amit

回答

0
<script type="text/javascript"> 
    jQuery(document).ready(function(){ 
     jQuery('img').each(function(index,value){ 
      var img = jQuery(this); 
      if(img.height == 80 && img.width == 80){ 
       img.attr("src", "/2/files/no_image.jpg"); 
      }console.log("Not found") 
     }); 
    }); 
</script>