Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery切换图像src属性_Jquery - Fatal编程技术网

jquery切换图像src属性

jquery切换图像src属性,jquery,Jquery,当我mouseover.thumb时,我希望src被hello.gif替换,当我mouseout时,我希望src恢复到原始值。第一部分很简单,但对于第二部分,我不知道如何“记住”原始src值 $('.thumb').mouseover(function() { current_src = $(this).attr("src"); $(this).attr("src", basepath+"_img/hello.gif"); }); $('.thumb').mouseout(fu

当我
mouseover
.thumb
时,我希望src被
hello.gif
替换,当我
mouseout
时,我希望src恢复到原始值。第一部分很简单,但对于第二部分,我不知道如何“记住”原始src值

$('.thumb').mouseover(function() {
    current_src = $(this).attr("src");
    $(this).attr("src", basepath+"_img/hello.gif");
});

$('.thumb').mouseout(function() {
    $(this).attr("src", ???);
});

您可以使用jquery的
data
方法


您可以使用jquery的
数据
方法


我为此编写了一个小型jQuery插件:

(function( $ ) {
    jQuery.fn.toggleSource = function(img){
        return this.each(function() {
        if(this.tagName.toLowerCase() != 'img') return;
            var $this = $(this);
            var orig = $this.attr('src');
            $this.mouseover(function(){
                $this.attr('src',img);
            });
            $this.mouseout(function(){
                $this.attr('src', orig);
            });
        });
    }
})(jQuery);
您可以这样使用它:

$('.thumb').toggleSource('http://my.site/my.img');

我为此编写了一个小型jQuery插件:

(function( $ ) {
    jQuery.fn.toggleSource = function(img){
        return this.each(function() {
        if(this.tagName.toLowerCase() != 'img') return;
            var $this = $(this);
            var orig = $this.attr('src');
            $this.mouseover(function(){
                $this.attr('src',img);
            });
            $this.mouseout(function(){
                $this.attr('src', orig);
            });
        });
    }
})(jQuery);
您可以这样使用它:

$('.thumb').toggleSource('http://my.site/my.img');
这样好多了

$('.hover').mouseover(function() {
    $(this).data('src', $(this).attr("src"));
    $(this).attr("src", $(this).attr("data-hover"));
});

$('.hover').mouseout(function() {
    $(this).attr("src", $(this).data('src'));
});
只需将数据悬停在imagen上

<img src="http://placehold.it/300x200" alt="" class="hover" data-hover="http://placehold.it/350x250" />

这样更好

$('.hover').mouseover(function() {
    $(this).data('src', $(this).attr("src"));
    $(this).attr("src", $(this).attr("data-hover"));
});

$('.hover').mouseout(function() {
    $(this).attr("src", $(this).data('src'));
});
只需将数据悬停在imagen上

<img src="http://placehold.it/300x200" alt="" class="hover" data-hover="http://placehold.it/350x250" />


有什么理由不能使用CSS psuedo类吗?具体来说,您可以使用:hover。有什么原因不能使用CSS psuedo类吗?具体来说,您可以使用:hover。