【问题标题】:How to switch src of an img tag in jquery如何在jquery中切换img标签的src
【发布时间】:2018-08-03 18:24:08
【问题描述】:

我在 HTML 中有一个带有源图像的 img 标记;然后我会在鼠标悬停时将图像切换到另一个图像;正是我在 rel 属性中设置的图像。

然后在鼠标移出时,切换回源图像。

我写了这个脚本,但它不起作用;这个问题肯定是由于错误使用了“元素”,但我无法解决。

function hover(element) {
  $(element).fadeOut(1000, function(element) {
    element.setAttribute("src", element.attr("rel"));
  }).fadeIn(1000);
  return false;
}

function unhover(element) {}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="image" 
     src="http://www.google.com/logos/2011/trevithick11-hp.jpg" 
     rel="http://www.google.com/logos/2011/mothersday11-hp.jpg" 
     onmouseover="hover(this);" 
     onmouseout="unhover(this);" 
     />

解决后我将专注于 mouseout 事件

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您正在将普通的 JS 与 jQuery 混合。您要做的第一件事是删除内联事件处理程序。然后,使用这个:

$('#image').hover(function(){
    $(this).fadeOut(1000, function() {
        $(this).attr( "src", $(this).attr("rel") );
    }).fadeIn(1000);
},function(){})

$('#image').hover(function() {
  $(this).fadeOut(1000, function() {
    $(this).attr("src", $(this).attr("rel"));
  }).fadeIn(1000);
}, function() {})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="image" src="http://www.google.com/logos/2011/trevithick11-hp.jpg" rel="http://www.google.com/logos/2011/mothersday11-hp.jpg" />

如果你也想处理鼠标移出,试试这个:

$('#image').hover(function() {
  $(this).stop().fadeOut(1000, function() {
    $(this).attr({
      "src": $(this).attr("rel"),
      "rel": $(this).attr("src")
    });
  }).fadeIn(1000);
}, function() {
  $(this).stop().fadeOut(1000, function() {
    $(this).attr({
      "src": $(this).attr("rel"),
      "rel": $(this).attr("src")
    });
  }).fadeIn(1000);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="image" src="http://www.google.com/logos/2011/trevithick11-hp.jpg" rel="http://www.google.com/logos/2011/mothersday11-hp.jpg" />

【讨论】:

    【解决方案2】:
    $('#img1').mouseover(function(){
      $('#img1').attr("src","new_source");
    
    });
    $('#img1').mouseout(function(){
      $('#img1').attr("src","old_source");
    
    });
    

    你可以看到它直播here

    【讨论】:

      【解决方案3】:

      试试这个:它会帮助你...

      function hover(element) {
          $("#image").fadeOut(200, function() {
              element.src= "http://www.google.com/logos/2011/mothersday11-hp.jpg";
          }).fadeIn(200);
        return false;
      }
      
      function unhover(element) {
                  element.src= "http://www.google.com/logos/2011/trevithick11-hp.jpg";
      }
      

      【讨论】:

        【解决方案4】:

        var original_src = null;
        
        $('#image').hover(
          // when mouse is over
          function(){
            original_src = $(this).attr('src');
            $(this).attr("src", $(this).attr("rel"));
          },
          // when mouse is out
          function(){
            $(this).attr("src", original_src);
          }
        );
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <img id="image" 
             src="http://www.google.com/logos/2011/trevithick11-hp.jpg" 
             rel="http://www.google.com/logos/2011/mothersday11-hp.jpg" 
             />

        这是一个动画示例:

        $(function(){
          var image = $('#image'),
              src = image.attr('src'),
              rel = image.attr('rel'),
              cloned_image = image.clone(),
              speed = 300;
            
          cloned_image.
          attr('src', rel).
          attr('id', image.attr('id')+'-clone').
          css({
            position: 'fixed',
            left: image.position().left,
            top: image.position().top,
            display: 'none'
          }).
          insertAfter(image);
          
          image.mouseover(
            function(){
              cloned_image.stop().fadeIn(speed);
              $(this).stop().fadeOut(speed);
            },
          );
          
          $('#'+image.attr('id')+'-clone').mouseout(function(){
            image.stop().fadeIn(speed);
            $(this).stop().fadeOut(speed);
          });
        });
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <img id="image" 
             src="http://www.google.com/logos/2011/trevithick11-hp.jpg" 
             rel="http://www.google.com/logos/2011/mothersday11-hp.jpg" 
             />

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-03
          • 2018-07-13
          • 1970-01-01
          • 2012-11-26
          • 2010-09-12
          • 1970-01-01
          • 2012-10-20
          • 1970-01-01
          相关资源
          最近更新 更多