2011-07-06 42 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") 

希望这有助于