删除img src的一部分

用户名

我有20张图片和相同的20张图片,它们的颜色不同,名称相同,但第二张图片的后缀为“ _blu”->“ google.jpg”,另一张为后缀“ google_blu.jpg”,我将使用鼠标悬停来进行更改从一个到另一个。

我尝试过这种方法,但是我不知道如何定位图像(我可以说“ .hovertondo类中的每个图像”)

jQuery(document).ready(function($) {
$(".hovertondo").css("opacity","1");

    $(".hovertondo").hover(function () {
        var src = $(this).attr('src');
        $(this).attr('src', src.replace('_blu', ''));
        $(this).stop().animate({opacity: 0}, "slow");

        },function () {$(this).stop().animate({opacity: 1}, "slow");
                       $(this).attr('src', src.replace('_blu', ''));

        });
});

的HTML

<div class="conttondodentosofia">
<img src="http://studiodentisticocova.com/img/trattamenti/carie_blu.jpg" />
<div class="hovertondo">Carie</div>
</div>

我用萤火虫打开了,错误是

“ TypeError:$(...)。attr(...)未定义”(谈论这行$(this).attr('src',src.replace('_ blu',''));)

有人帮我吗?:) 谢谢!

奥利维尔

src未在mouseleave处理程序中定义(hovermethod的第二个参数):

改变这个

$(".hovertondo").hover(function () {
        var src = $(this).attr('src');
        $(this).attr('src', src.replace('_blu', ''));
        $(this).stop().animate({opacity: 0}, "slow");

        },function () {$(this).stop().animate({opacity: 1}, "slow");
                       $(this).attr('src', src.replace('_blu', ''));

        });

$(".hovertondo").hover(function () {
        var src = $(this).attr('src');
        $(this).attr('src', src.replace('_blu', ''));
        $(this).stop().animate({opacity: 0}, "slow");

        },function () {
        var src = $(this).attr('src');
        $(this).stop().animate({opacity: 1}, "slow");
        $(this).attr('src', src.replace('_blu', ''));

        });

顺便说一句,您$(this).attr('src', src.replace('_blu', ''));在两种情况下都在做在某些情况下,您必须将_blu添加到您的src中。它会给出类似的信息:$(this).attr('src', src.replace('.jpg', '_blu.jpg'));

编辑

您可以使用fadeOut方法进行平滑的图像修改

$('this').fadeOut(300, function(){
      $(this).attr('src', src.replace('_blu', '')).bind('onreadystatechange load', function(){
         if (this.complete) $(this).fadeIn(300);
      });
   });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章