jquery - duplicate image with slightly different name -
i need duplicate image file , add after making slight change file name.
the problem having var not change each image. have run many more images have find way update var each image.
<a class="slashc-img-zoom-pan"> <img src="/v/vspfiles/photos/hk01344-1478-1.jpg"> </a> <a class="slashc-img-zoom-pan"> <img src="/v/vspfiles/photos/sc-00342-1.jpg"> </a> <a class="slashc-img-zoom-pan"> <img src="/v/vspfiles/photos/bs-0034-534-1.jpg"> </a> <script> $('a img').each(function() { var image_sku = $("a").html().split("-1.jpg")[0]; $(this).after(image_sku + '-2.jpg" >'); }); </script>
the outcome suppose this:
<a class="slashc-img-zoom-pan"> <img src="/v/vspfiles/photos/hk01344-1478-1.jpg"> <img src="/v/vspfiles/photos/hk01344-1478-2.jpg"> </a> <a class="slashc-img-zoom-pan"> <img src="/v/vspfiles/photos/sc-00342-1.jpg"> <img src="/v/vspfiles/photos/sc-00342-2.jpg"> </a> <a class="slashc-img-zoom-pan"> <img src="/v/vspfiles/photos/bs-0034-534-1.jpg"> <img src="/v/vspfiles/photos/bs-0034-534-2.jpg"> </a>
this jquery.
$('a.slashc-img-zoom-pan img:eq(0)').each(function(){ var src = $(this).attr('src'); var src2 = src.replace('-1.jpg','-2.jpg'); $('<img src="' + src2 +'" >').appendto('a.slashc-img-zoom-pan'); });
http://jsfiddle.net/jasongennaro/tuznf/3/
explanation:
- get first instance of img (necessary, since creating second)
- grab src text
- replace end bit
- add parent
a
Comments
Post a Comment