jquery - Manipulating a copied attr -
i'm working on simple lightbox script website, wraps link (a) around image. link url should src of image, manipulated.
the image url this: "pb028855_copy_147x110.jpg" , want delete "147x110", after "copy" before ".jpg" is.
my current script, copy src image link this:
$(function(){ $('img.withcaption').each(function(){ var $imgsrc = $(this).attr("src"); $(this).wrap('<a rel="lightbox" />'); $(this).parent().attr("href", ""+$imgsrc); }); });
how can use parts of attr?
thank in advance...
if you're not numbers be, use replace()
(docs) method regular expression.
$(function(){ $('img.withcaption').each(function(){ var $imgsrc = this.src.replace(/_\d+x\d+\./,'.'); $(this).wrap('<a rel="lightbox" />') .parent().attr("href", ""+$imgsrc); }); });
so this:
pb028855_copy_147x110.jpg
will end this:
pb028855_copy.jpg
Comments
Post a Comment