Javascript 使用jQuery删除部分文件名

Javascript 使用jQuery删除部分文件名,javascript,jquery,replace,strip,Javascript,Jquery,Replace,Strip,我想删除如下文件名: IMG_6903.JPG&width=504 致: IMG_6903.JPG 我的脚本如下所示: $(function(){ $('#exposure img').each(function(){ var $imgSrc = this.src.split('&'); $(this).wrap('<a rel="lightBox" />') .parent().attr("href

我想删除如下文件名: IMG_6903.JPG&width=504

致:

IMG_6903.JPG

我的脚本如下所示:

  $(function(){
    $('#exposure img').each(function(){
        var $imgSrc = this.src.split('&');
        $(this).wrap('<a rel="lightBox" />')
               .parent().attr("href", ""+$imgSrc);
      });
  });    
$(函数(){
$(“#暴露img”)。每个(函数(){
var$imgSrc=this.src.split('&');
$(此).wrap(“”)
.parent();
});
});    

但它不起作用。。。我如何做到这一点?

使用拆分

var andIndex = tmpString.indexOf('&');
tmpString = tmpString.substr(0, andIndex);
var str = 'IMG_6903.JPG&width=504';
var array = str.split('&');
array[0];

数组[0]将是IMG_6903。JPG

拆分字符串,但尝试使用数组而不是第一个元素。试试这个

 $(function(){
     $('#exposure img').each(function(){
         var $imgSrc = this.src.split('&');
         $(this).wrap('<a rel="lightBox" />')
             .parent().attr("href", "" + $imgSrc[0]);
     });
 });
$(函数(){
$(“#暴露img”)。每个(函数(){
var$imgSrc=this.src.split('&');
$(此).wrap(“”)
.parent().attr(“href”,“+$imgSrc[0]);
});
});

希望这有帮助。

4分钟内回答3个正确答案。。。哇!是时候接受一个了o) 只是想知道,你确定它不是
IMG_6903.JPG?width=504
var imgSrc = this.src.substring(0,this.src.indexOf('&'));