jQuery 点击切换图像源

3
我整理了这段代码:

我把这段代码放在这里:

$('img').on({
    'click': function() {
         var src = ($(this).attr('src') === 'memes/2a.png')
             ? 'memes/2b.png'
             : 'memes/2a.png';
         $(this).attr('src', src);
     }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<a href="#"><img id="img1" src="memes/2a.png" height="500" width="600"></a>

当我点击它时,它应该在两张图片(2a.png和2b.png)之间切换,如果没有反应,那么就出了问题。

有人可以告诉我我做错了什么吗?

2个回答

4

这里的主要问题在于你的锚点元素,它会导致页面重新加载。

简单地移除锚点元素并将图像设为指针光标即可。

没有锚点a,有指针在图像上。

$('img').on({
    'click': function() {
         document.querySelector('span').textContent = $(this).attr('src');
         var src = ($(this).attr('src') === 'animals/5/')
             ? 'animals/6/'
             : 'animals/5/';
         $(this).attr('src', src);
     }
});
#img1 {
  cursor: pointer;
}

span {
  vertical-align: top;
  padding: 5px 10px;
  background: black;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<base href="http://lorempixel.com/300/300/">
<img id="img1" src="animals/5/" height="300" width="300">
<span></span>

使用锚点a时,可能会出现不正常的情况。

$('img').on({
    'click': function() {
         document.querySelector('span').textContent = $(this).attr('src');
         var src = ($(this).attr('src') === 'animals/5/')
             ? 'animals/6/'
             : 'animals/5/';
         $(this).attr('src', src);
     }
});
span {
  vertical-align: top;
  padding: 5px 10px;
  background: black;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<base href="http://lorempixel.com/300/300/">
<a href="#"><img id="img1" src="animals/5/" height="300" width="300"></a>
<span></span>

另一个选择是取消事件冒泡,保留锚点a,但我仍建议将其简单删除。

$('img').on({
    'click': function(e) {
         document.querySelector('span').textContent = $(this).attr('src');
         var src = ($(this).attr('src') === 'animals/5/')
             ? 'animals/6/'
             : 'animals/5/';
         $(this).attr('src', src);
         e.stopPropagation();
         return false;
     }
});
span {
  vertical-align: top;
  padding: 5px 10px;
  background: black;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<base href="http://lorempixel.com/300/300/">
<a href="#"><img id="img1" src="animals/5/" height="300" width="300"></a>
<span></span>


感谢您的帮助,我选择删除锚点并使用CSS指针类。现在它完美地按照我的意愿工作。 - AtomiCookiez

0

正如其他人所指出的那样,这很可能是由于相对路径问题。如果您不想使用绝对路径,可以在图像上保留一个数据属性以了解如何处理切换。

$('img').on({
  'click': function() {

    var i = $(this).attr('data-index');
    switch (i) {
      case '0':
        i = '1';
        src = 'https://unsplash.it/100/100?image=1';
        break;
      case '1':
      default:
        i = '0';
        src = 'https://unsplash.it/100/100?image=0';
        break;
    }

    $(this)
      .attr('src', src)
      .attr('data-index', i);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<a href="#">
  <img id="img1" data-index='1' src="https://unsplash.it/100/100?image=1">
</a>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接