开发者

Jquery .next() not working

开发者 https://www.devze.com 2023-03-18 03:22 出处:网络
I have table structure with the following. <td c开发者_开发百科lass=\"backgroundimage\"><img src=\"02.jpg\" border=\"0\" class=\"foregroundimage\"></td>

I have table structure with the following.

<td c开发者_开发百科lass="backgroundimage"><img src="02.jpg" border="0" class="foregroundimage"></td>
<td class="backgroundimage"><img src="03.jpg" border="0" class="foregroundimage"></td>

I am trying to get each img src within my table by doing this.

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

    alert(oldImage);
});

This alerts undefined. What have I done wrong? Am I using .next() wrong?


Yes - .next() looks at the next sibling. And none of your td elements have an img sibling.

You probably wanted to use $(this).find('img') or simply $('img', this).

Depending on what you need to do the following might also do the job:

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


Instead of:

$(this).next("img")

You should do:

$(this).find("img")

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消