用javascript替换他人的图像

时间:2015-02-25 23:15:22

标签: javascript image replace

我如何用其他人用javascript替换图片?

我已经尝试但是出了点问题



 if ($(".grimm")) {
   $('.ch').
     html(
       $('.ch').
         html().
         replace('http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png',
                 'http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mails-icon.png')
   );
 }

<div class="grimm">
  <div class="ch">
    <img src="http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png" />
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

确保包含jQuery,然后您的小提琴正常工作:http://jsfiddle.net/dm9xkmkd/1/

但是您也可以在不涉及replace()或重写大块HTML的情况下一步处理:

&#13;
&#13;
$('.grimm .ch img[src="http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png"]').
  attr('src', 'http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mails-icon.png');
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="grimm">
  <div class="ch">
    <img src="http://icons.iconarchive.com/icons/graphicrating/koloria/32/Mail-icon.png" />
  </div>
</div>
&#13;
&#13;
&#13;