0

代码是这样的:

$(' html body div.wrapper div.middle div.post_home:first-child a ').hover(
function() {
        $(' html body div.wrapper div.middle div.post_home:first-child a > img ').attr("src", "http:/site/test1.png");
},
 function() {
        $('html body div.wrapper div.middle div.post_home:first-child a > img ').attr("src", "currentSrc");
});

我只需要在更改之前保留图像的 src 值,以便在 mouseleave 上检索图像并显示它。

4

4 回答 4

1

您可以使用data属性来存储您的 src 以便以后检索:

var thisImg;
$('.post_home:first-child a').hover(function () {
    thisImg = $('img', this)[0];
    $(this).data('img-src', thisImg.src);
    thisImg.src = "http:/site/test1.png";
}, function () {
    thisImg.src = $(this).data('img-src');
});

演示

于 2013-03-03T23:29:05.803 回答
1

src在更改之前在活动开始时备份您的属性。并在您想返回时使用它。

var currentSrc;
$('.post_home:first-child a ').hover(function() {

    currentSrc = $('.post_home:first-child a > img').attr("src");
    $('.post_home:first-child a > img ').attr("src", "http:/site/test1.png");

}, function() {

        $('.post_home:first-child a > img').attr("src", currentSrc);

});

注意:我编辑了您的选择器,因为您所拥有的已经超出了必要的范围。

于 2013-03-03T23:24:31.037 回答
0

您可以将原始 src 缓存在变量中以供以后使用。像这样的东西:

var originalSrc = $(' html body div.wrapper div.middle div.post_home:first-child a > img ').attr('src');
$(' html body div.wrapper div.middle div.post_home:first-child a ').hover(
function() {
        $(' html body div.wrapper div.middle div.post_home:first-child a > img ').attr("src", "http:/site/test1.png");
},
 function() {
        $('html body div.wrapper div.middle div.post_home:first-child a > img ').attr("src", originalSrc);
});
于 2013-03-03T23:24:07.807 回答
0

尝试使用 HTML5数据属性,这样您就可以将原始 url 存储在“src”道具和data-src="my other image"您想要更改的图像中。

<img src="my original image" data-src="my other src" />
于 2013-03-03T23:25:10.150 回答