Change the image source on rollover using jQuery

前端 未结 14 1243
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 10:23

I have a few images and their rollover images. Using jQuery, I want to show/hide the rollover image when the onmousemove/onmouseout event happen. All my image names follow t

14条回答
  •  暖寄归人
    2020-11-22 10:50

    I've made something like the following code :)

    It works only, when you have a second file named with _hover, for example, facebook.png and facebook_hover.png

    $('#social').find('a').hover(function() {
        var target = $(this).find('img').attr('src');
        //console.log(target);
        var newTarg = target.replace('.png', '_hover.png');
        $(this).find('img').attr("src", newTarg);
    }, function() {
        var target = $(this).find('img').attr('src');
        var newTarg = target.replace('_hover.png', '.png');
        $(this).find('img').attr("src", newTarg);
    });
    

提交回复
热议问题