javascript - jQuery 在悬停时交换图像,然后在悬停时将其替换为原始图像

标签 javascript jquery html

我正在使用以下代码在 child 的 parent 悬停时换出图像 src。如何将原始来源保存在变量中并在悬停时将图像返回到其原始来源?

      $("#parent span").hover(function(){
          var currentImage = $('img', this ).attr("src","images/orbs/yellowBar.png");
          $('img', this ).attr("src","images/yellowBar.png");


        },function(){

          $('img', this ).attr("src",currentImage);

      });

最佳答案

在函数之外定义 currentImage:

var currentImage;

$("#parent span").hover(function() {
    currentImage = $('img', this).attr("src", "images/orbs/yellowBar.png");
    $('img', this).attr("src", "images/yellowBar.png");
}, function() {
    $('img', this).attr("src", currentImage);
});

关于javascript - jQuery 在悬停时交换图像,然后在悬停时将其替换为原始图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29545652/

相关文章:

javascript - 无法让 jQuery UI 模态对话框成为模态

javascript - 在完整文档中查找最接近的元素

javascript - 为什么第二个对话打不开?

html - 内容溢出时如何使 <td> 换行?

jQuery:同时选择一个元素的类和 ID?

JavaScript:在 DOM 元素后附加空格 ( )

javascript - 在没有 iframe 的情况下嵌入 HTML5 YouTube 视频?

javascript - 使用正则表达式拆分算术表达式

javascript - 如何创建 Div ID 数组

javascript - 如何在不丢失数据的情况下将 POST 正文数据传递到另一条路线?