2011-07-06 9 views
5

Tengo estructura de tabla con lo siguiente.Jquery .next() no funciona

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

Estoy tratando de obtener cada img src dentro de mi mesa haciendo esto.

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

    alert(oldImage); 
}); 

Esto alerta indefinido. ¿Qué he hecho mal? ¿Estoy usando .next() mal?

Respuesta

5

Sí - .next() mira al siguiente hermano. Y ninguno de sus elementos td tiene un hermano img.

Probablemente haya querido usar $(this).find('img') o simplemente $('img', this).

Dependiendo de lo que tiene que hacer lo siguiente también podría hacer el trabajo:

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

Gracias find() funcionó muy bien . – james

3

En lugar de:

$(this).next("img") 

que debe hacer:

$(this).find("img") 

Espero que esto ayude