4

我能做些什么来实现这一点。我希望如果我单击该图像,该imgThumb1图像的 src 会复制到 上mainIMG,但是当我单击它时图像不会改变...我该怎么办?

$(document).ready(function(){
    $('.propertyResultImageThumb img[alt="imgThumb1"]').click(function(){
        var trial = $('img[alt="imgThumb1"]').attr('src');
        $("#mainIMG").attr("src", trial);
    });
});


<img alt="mainIMG" src="" id="mainIMG"/>
<img alt="imgThumb1" src="http://..../uploads/2012/09/Villa-Located-In-Almansa.jpg" />
4

4 回答 4

5
$(document).ready(function(){
    $('img[alt="imgThumb1"]').click(function(){
        var trial = $('img[alt="imgThumb1"]').attr('src');
        $('img[alt="mainIMG"]').attr("src", trial);
    });
});

演示


我的代码

$(function(){
    $('#first').click(function(){
        $("#second").attr("src", $(this).attr('src'));
    });
});

<img src="" id="first"/>
<img src="" id="second" />
于 2012-09-14T06:09:43.997 回答
1

尝试将您的 html 包装在具有类 propertyResultImageThumb 的容器中

<div class="propertyResultImageThumb ">
<img alt="mainIMG" src="" id="mainIMG"/>
<img alt="imgThumb1" src="http://..../uploads/2012/09/Villa-Located-In-Almansa.jpg" />
</div>     

然后像这样

$(document).ready(function(){
    $('.propertyResultImageThumb img[alt="imgThumb1"]').click(function(){
        var trial = $(this).attr('src');
        $("#mainIMG").attr("src", trial);
    });
});
于 2012-09-14T06:05:24.620 回答
0

你需要有一个类名为“propertyResultImageThumb”的父dom元素检查jsfiddle

http://jsfiddle.net/erwKP/1/

于 2012-09-14T06:12:22.740 回答
0

如果你想复制一个图像而不需要再次从服务器下载(如果是验证码,它可能会改变),那么应该使用这个代码:

$(function(){
    $('#first').click(function(){
        img_copy(document.getElementById('#first'),document.getElementById('#second'));
    });
});

//copy src to destination without destination to download the image
function img_copy(src,destination)
{
     var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');
    context.drawImage(src, src.width, src.height);
    var dataURL = canvas.toDataURL();
    destination.src = dataURL;
}



<img src="" id="first"/>
<img src="" id="second" />
于 2015-05-26T10:48:22.887 回答