Замените img src, используя Jquery - PullRequest
0 голосов
/ 25 ноября 2018

Я пытаюсь изменить часть img src при наведении курсора, используя jquery (POST с MOBILE).Вот HTML-код

<a onclick="slideshow(7); return false;"href="http://example.com/12345">
<img src="https://DEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
</a>

, а вот jquery

$('a img').hover (function(){
    this.src = this.src.replace('post','mobile');
});

Возможно, я что-то не так делаю.Пожалуйста, помогите мне

1 Ответ

0 голосов
/ 25 ноября 2018

Вы должны поместить новую строку во второй параметр String.replace() вместо того, чтобы использовать его дважды.

$('a img').hover(function(){
  this.src = this.src.replace('post', 'mobile');
});

$('a img').hover(function(){
  this.src = this.src.replace('post', 'mobile');
  console.log(this.src);
});
img {
  width: 100px;
  height: 100px;
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="http://example.com/12345">
  <img src="http://aDEF.com/123-post.jpg" width="200" heigh="200" class="fish" id="thumbnail_5">
</a>
...