我正在使用以下代码在 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/