开发者

Remove part of filename with jQuery

开发者 https://www.devze.com 2023-02-17 08:18 出处:网络
I want to strip a filename like this: IMG_6903.JPG&width=504 to: IMG_6903.JPG ... My script looks like this:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消