Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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
Javascript 在jQuery中使用attr()后,如何向图像添加效果? 处境_Javascript_Jquery - Fatal编程技术网

Javascript 在jQuery中使用attr()后,如何向图像添加效果? 处境

Javascript 在jQuery中使用attr()后,如何向图像添加效果? 处境,javascript,jquery,Javascript,Jquery,我有一个btn和图像。 我想在用户单击btn时更改图像的src 然后,我想给它添加一点效果,比如fadeIn('slow') 我试过了 追加myfadeIn('slow')在我的attr()的末尾,但我没有发生任何影响 $('#id').click(function(e) { e.preventDefault(); $('#img').attr('src','/path/car.png').fadeIn('slow'); <-----HERE }); 有人能帮我纠

我有一个btn图像。 我想在用户单击btn时更改图像的src

然后,我想给它添加一点效果,比如
fadeIn('slow')


我试过了 追加my
fadeIn('slow')在我的
attr()
的末尾,但我没有发生任何影响

$('#id').click(function(e) {
    e.preventDefault();

    $('#img').attr('src','/path/car.png').fadeIn('slow');  <-----HERE

});

有人能帮我纠正这个问题吗?

首先使用
fadeOut
,一旦完成使用
fadeIn

$('#id')。单击(函数(e){
e、 预防默认值();
$('#img').fadeOut('slow',function(){
$('#img').attr('src','http://icons.iconarchive.com/icons/crountch/one-piece-jolly-roger/256/Zoro-icon.pngfadeIn('slow');
});
});

单击我

在这里,我做了一个示例,您可以多次更改图像的src:


希望有帮助。

默认情况下图像是隐藏的吗?如果图像已经可见,它将不会“重新淡入”。这对我来说很有效:图像会改变吗?如果没有,你能提供相关的HTML吗?@jack:那我怎么解决呢?一定有办法。“你能帮我吗?”华盛顿:我看见了。你说得对。隐马尔可夫模型。。。所以我可以把它锁起来。让我再调查一下。非常感谢!是
.finish().hide()
这里的键是吗?@rangerover.js是的,它们是。finish()将结束当前正在运行的任何fadeIn()效果。。。and.hide()将为img准备新的fadeIn()效果。
$('#id').click(function(e) {
    e.preventDefault();

    $('#img').attr('src','/path/car.png');
    $('#img').fadeIn('slow');  <-----HERE

});
$("#b1").on("click", function(){
    $("img")
        .finish().hide() // pay attention on this line -> it will end any fadeIn and then hide to do a new fadeIn
        .attr("src", "https://www.petfinder.com/wp-content/uploads/2012/11/dog-how-to-select-your-new-best-friend-thinkstock99062463.jpg")
        .fadeIn(2000);
});

$("#b2").on("click", function(){
    $("img")
        .finish().hide() // pay attention on this line -> it will end any fadeIn and then hide to do a new fadeIn
        .attr("src", "http://blogs.psychcentral.com/life-goals/files/2014/09/cute-dog-pup.jpg")
        .fadeIn(2000);
});

$("#b3").on("click", function(){
    $("img")
        .finish().hide() // pay attention on this line -> it will end any fadeIn and then hide to do a new fadeIn
        .attr("src", "http://upload.wikimedia.org/wikipedia/commons/1/13/Clyde_The_Bulldog.jpg")
        .fadeIn(2000);
});