2013-01-12 58 views
1

我想编辑所有图像的src属性。我实际上想从所有图像src中删除“\ test”。我试过在jQuery中替换和src.replace,但它没有成功。请帮助如何做到这一点。使用jquery编辑图像src

$("img").each(function(){ 
      var srcVar = this.attr('src'); 
      this.attr('src', "http://www.abc.gr"+srcVar); 
     }); 
+0

+ srcVar.split(“/测试“)[1] – mplungjan

回答

0

使用$(this)代替:

$("img").each(function(){ 
    var srcVar = $(this).attr('src').replace("/test", "");; 
    $(this).attr('src', srcVar); 
}); 
1

这会帮助你:

$("img").each(function(){ 

    // get the current source, replace the test string 
    var srcVar = this.attr('src').replace('/test', ''); 

    // apply it back 
    $(this).attr('src', srcVar); 
}); 
+0

nope无变化:( – james

1

尝试了这一点...

$("img").each(function(){ 
    var srcVar = $(this).attr('src'); 
    srcVar = srcVar.replace("/test", ""); 
    $(this).attr('src', srcVar); 
});