0

次のコードを取得しました。

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);
})​

フィドル

下の画像をクリックしたときにsrc、上の画像の を下の画像の に置き換えて、src両方の画像が同じになるようにします。

提供されたコードが機能しないのはなぜですか?

4

2 に答える 2

4

これを試してください:

$(".photo").click(function() {
    var src=$(this).children("img").attr("src");
    $(".image img").attr("src",src);
})​;
于 2012-07-24T23:54:10.230 に答える
0

これを試して

You need to get and set the src attribute, using attr() function in jQuery

于 2012-07-24T23:55:50.760 に答える