Remove part of filename with jQuery
I want to strip a filename like this: IMG_6903.JPG&width=504
to:
IMG_6903.JPG
...
My script looks like this:
$(function(){
$('#exposure img').each(function(){
var $imgSrc = this.src.split('&');
$(this).wrap('<a rel="lightBox" />')
开发者_StackOverflow .parent().attr("href", ""+$imgSrc);
});
});
But it doesn't work... How do I do this?
var imgSrc = this.src.substring(0,this.src.indexOf('&'));
var andIndex = tmpString.indexOf('&');
tmpString = tmpString.substr(0, andIndex);
use split
var str = 'IMG_6903.JPG&width=504';
var array = str.split('&');
array[0];
array[0] will be IMG_6903.JPG
You split the string but try using the array instead of the first element. Try this
$(function(){
$('#exposure img').each(function(){
var $imgSrc = this.src.split('&');
$(this).wrap('<a rel="lightBox" />')
.parent().attr("href", "" + $imgSrc[0]);
});
});
Hope this helps.
精彩评论