2011-07-06 46 views
5

我有以下表結構。Jquery .next()不工作

<td class="backgroundimage"><img src="02.jpg" border="0" class="foregroundimage"></td> 
<td class="backgroundimage"><img src="03.jpg" border="0" class="foregroundimage"></td> 

我想通過這樣做我的表中的每個img src。

$('.backgroundImage').each(function(index){ 
    var oldImage = $(this).next("img").attr('src'); 

    alert(oldImage); 
}); 

此警報未定義。我做錯了什麼?我是否使用.next()錯誤?

回答

5

是的 - .next()看着下一個兄弟姐妹。並且您的td元素都沒有img兄弟姐妹。

您可能想要使用$(this).find('img')或者簡單地使用$('img', this)

取決於你需要做以下也可以做的工作是什麼:

$('.backgroundimage img').each(function() { 
    var oldImage = $(this).attr('src'); 
}); 
+0

Thanks find()worked great 。 – james

3

相反的:

$(this).next("img") 

你應該這樣做:

$(this).find("img") 

希望這有助於