jquery iterate over child elements

后端 未结 3 1788
北海茫月
北海茫月 2021-02-05 02:11

I have a div with the id ring-preview, it has a unspecifed number of img elements with the class stone-preview inside it.

相关标签:
3条回答
  • 2021-02-05 02:55

    You can use a .each() in these cases, like this:

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

    The first parameter to the callback function is the index you're after.

    0 讨论(0)
  • 2021-02-05 03:09

    You're looking for the .each() method.
    For example:

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

    If the <img> elements aren't direct children, you'll need to call .find instead of .children.

    0 讨论(0)
  • 2021-02-05 03:11
    $('#ring-preview img.stone-preview').each(function(idx, itm) {
        $(itm).rotate(stones[idx].stone_rotation);
    });
    
    0 讨论(0)
提交回复
热议问题