简体   繁体   中英

jQuery .each - run function for each instance of selector

I'm trying to create a simple Flickr plugin via the API to show a gallery of photo sets . The way I'd like it to work is something like this:

    <div id="72157634235266773" class="thumb"></div>

    <div id="72157633471728555" class="thumb"></div>

and the jQuery plugin would iterate through and find all div's with class="thumb" and plug the id value - which is the flickr photoset id - into the code to render the thumbnail and accompanying image files for the gallery.

This fiddle has my code: http://jsfiddle.net/eBGVV/3/

See how it places two of the same thumbnail images next to each other under "Photo Set 1"?

The desired result would be the correct thumb under each Photo Set 1 and 2.

I know there is a problem with how I'm using the .each but I'm not sure what?

Thanks!

Change the first four lines so the $(document).ready nests everything, and within the each function, you only want to refer to the current iterated instance of the .thumb using $(this)

$(document).ready(function () {
     $('.thumb').each(function () {
          var PhotoSetID = $(this).attr("id");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM