javascript - Won't Iterate Over Array In jQuery -
so can of images want array , pass them $image. when try loop on array keeps alerting same item 3 times.
the code i'm having trouble with.
getitem : function($image){ console.log($image) console.log(jquery.type($image)) var setup ='<img src="' + $($image).attr('href') + '" title="' + $($image).attr('title') + '"/>'; $.each($image, function(i){ alert( setup); }); }
the html
<a href="images/slideshow/1-gw.phillipbarnhart.reverendmemory.jpg" title="phillip barnhart as: reverend memory - clergyman stands decorum , truth." rel="slideshow"><img src="images/view-slideshow.jpg" width="490" height="352" alt="view slideshow"></a> <a rel="slideshow" href="images/slideshow/2-gw.bethbrooks.pollytodd.jpg">fff</a> <a rel="slideshow" href="images/slideshow/3-gw.nickhale.nostalgia.jpg">test</a>
the whole script or if jsfiddle here link. http://jsfiddle.net/h3az4/
var slideshow = { config : { wrapper : 'body', container : 'div', anchor : 'a[rel="slideshow"]' }, init : function(config) { $.extend(slideshow.config, config); $(slideshow.config.anchor).hide(); $(slideshow.config.wrapper).find(slideshow.config.anchor) .eq(0) .show() .click(function(e){ e.preventdefault(); slideshow.getitem($(slideshow.config.anchor)); }); }, getitem : function($image){ console.log($image) console.log(jquery.type($image)) var setup ='<img src="' + $($image).attr('href') + '" title="' + $($image).attr('title') + '"/>'; $.each($image, function(i){ alert( setup); }); }, createtumbnail : function($image){ } }; $(document).ready(function() { slideshow.init(); });
your using $.each loop wrong.
your first problem $image.attr("x") attr of first element in list if $image list. want either $($image[i]) or using .get
the second issue declaring var setup
outside loop. means declared , used once rather 3 times (since have 3 items).
$.each($image, function(i){ var setup ='<img src="' + $(this).attr('href') + '" title="' + $(this).attr('title') + '"/>'; alert( setup); });
when using $.each
this
object in function refer each object in array in turn. in case this
dom object want use $(this)
jquery image object.
look here working example http://jsfiddle.net/raynos/h3az4/3/
Comments
Post a Comment