【问题标题】:jquery onclick toggle image srcjquery onclick 切换图像 src
【发布时间】:2017-06-06 14:48:33
【问题描述】:

我在这里整理了这段代码:

$('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 当我点击它时没有任何反应。

谁能告诉我我做错了什么?

【问题讨论】:

    标签: javascript jquery html web toggle


    【解决方案1】:

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

    只需删除锚元素并给图像一个指针光标

    没有锚点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指针类。它现在就像我想要的那样完美运行。
    【解决方案2】:

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

    $('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>

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 2013-10-16
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      相关资源
      最近更新 更多