jQuery Loop through each div

后端 未结 5 803
暗喜
暗喜 2021-02-05 15:10

I\'m pretty sure this will be a really easy answer for you jQuery whizzes, and I\'m also pretty such it involves a loop of some kind.

I\'m trying to perform essentially

5条回答
  •  暖寄归人
    2021-02-05 15:32

    You're right that it involves a loop, but this is, at least, made simple by use of the each() method:

    $('.target').each(
        function(){
            // iterate through each of the `.target` elements, and do stuff in here
            // `this` and `$(this)` refer to the current `.target` element
            var images = $(this).find('img'),
                imageWidth = images.width(); // returns the width of the _first_ image
                numImages = images.length;
            $(this).css('width', (imageWidth*numImages));
    
        });
    

    References:

    • css().
    • each().
    • find().

提交回复
热议问题