Change the image source on rollover using jQuery

前端 未结 14 1021
隐瞒了意图╮
隐瞒了意图╮ 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 11:02

    To set up on ready:

    $(function() {
        $("img")
            .mouseover(function() { 
                var src = $(this).attr("src").match(/[^\.]+/) + "over.gif";
                $(this).attr("src", src);
            })
            .mouseout(function() {
                var src = $(this).attr("src").replace("over.gif", ".gif");
                $(this).attr("src", src);
            });
    });
    

    For those that use url image sources:

    $(function() {
            $("img")
                .mouseover(function() {
                   var src = $(this).attr("src");
                   var regex = /_normal.svg/gi;
                   src = this.src.replace(regex,'_rollover.svg');
                   $(this).attr("src", src);
    
                })
                .mouseout(function() {
                   var src = $(this).attr("src");
                   var regex = /_rollover.svg/gi;
                   src = this.src.replace(regex,'_normal.svg');
                   $(this).attr("src", src);
    
                });
        });
    

提交回复
热议问题