jquery遍历子元素

时间:2010-11-05 19:35:16

标签: jquery

我的div标识为ring-preview,其中包含imgstone-preview个元素,其中包含$(this).rotate(ring.stones[i].stone_rotation); 个类。

我想迭代这些子图像并调用:

this

img引用i元素,div引用{{1}}中的位置。

我该怎么做?

3 个答案:

答案 0 :(得分:55)

您正在寻找.each() method 例如:

$('.ring-preview').children('img').each(function(i) { 
    $(this).rotate(ring.stones[i].stone_rotation);
});

如果<img>元素不是直接子女,则需要拨打.find而不是.children

答案 1 :(得分:8)

在这些情况下,您可以使用.each(),如下所示:

$("#ring-preview img.stone-preview").each(function(i) {
  $(this).rotate(ring.stones[i].stone_rotation);
});

回调函数的第一个参数是你所追求的索引。

答案 2 :(得分:8)

$('#ring-preview img.stone-preview').each(function(idx, itm) {
    $(itm).rotate(stones[idx].stone_rotation);
});