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。