.replace() not returning
I'm real noob at jQuery, this looks like a simple modification but it's not working. I am just trying to insert _thumb into the filename in a jQuery plugin that is otherwise already working.
//work开发者_如何学Cing
return '<li class="thumb"><a href="#"><img src="' + jQuery(slide).find('img').attr('src') + '" height="75" /></a></li>';
//NOT working
return '<li class="thumb"><a href="#"><img src="' + jQuery(slide).find('img').attr('src', src.replace('.jpg','_thumb.jpg')) + '" height="75" /></a></li>';
I bet you if you check your javascript console or error log it'll say something like src is undefined
Try:
return '<li class="thumb"><a href="#"><img src="' + jQuery(slide).find('img').attr('src').replace('.jpg','_thumb.jpg') + '" height="75" /></a></li>';
This is because .replace()
works on a string, the string object in this case is the $().find().attr()
object, so you need to execute .replace()
on that object, as in the example.
Have you tried this:
return '<li class="thumb"><a href="#"><img src="' + jQuery(slide).find('img').attr('src').replace('.jpg','_thumb.jpg') + '" height="75" /></a></li>';
As @Ish commented, src
is undefined. Did you mean something like this?
var src = jQuery(slide).find('img').attr('src').replace('.jpg','_thumb.jpg');
return '<li class="thumb"><a href="#"><img src="' + src + '" height="75" /></a></li>';
精彩评论