动态改变图像源 - jQuery的

问题描述:

我得到了下面的代码:动态改变图像源 - jQuery的

HTML

<div class="image"> 
    <img src="http://ecx.images-amazon.com/images/I/41ghiuvjdvL._SL75_SS50_.jpg" /> 
</div> 

<br /> 

<div class="photo"> 
    <img src="http://profile.ak.fbcdn.net/hprofile-ak-ash2/27520_119569768054263_2432_q.jpg" /> 
</div>​ 

JS

// getting src of bottom image 
var bottomSrc = $(".photo img").find("src"); 

// getting src of top image 
var topSrc = $(".image img").find("src"); 

$(".photo").click(function() { 
    // changing sources 
    // topSrc.attr("src", bottomSrc); 
    // topSrc.replaceWith(bottomSrc); 
    topSrc.replace(bottomSrc); 
})​ 

Fiddle

当底部图像被点击,我想顶部图像的src被替换成底部图像的src,这样两个图像都是相同的。

为什么提供的代码不起作用?

+0

要使用'.attr您需要jQuery来获取和设置src属性,使用ATTR()函数('SRC ')'获取src属性的值,而不是'.find()',它可以找到子元素 – MrOBrian 2012-07-24 23:53:52

尝试了这一点:

$(".photo").click(function() { 
    var src=$(this).children("img").attr("src"); 
    $(".image img").attr("src",src); 
})​;