jQuery - Change image src on hover

前端 未结 6 967
自闭症患者
自闭症患者 2020-12-10 14:28

Basically I want to change the image src to add -active.png on hover.

So fb.png would become fb-active.png on hover, and fb.png when it\'s not hovering.

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 15:17

    You could do this just in CSS if you don't need to be compliant with older browsers.

    To do it in jquery, you can try something like this:

    $(".img-social").hover(function(){
        $(this).attr("src", function(index, attr){
            return attr.replace(".png", "-active.png");
        });
    }, function(){
        $(this).attr("src", function(index, attr){
            return attr.replace("-active.png", ".png");
        });
    });
    

提交回复
热议问题