2013-01-12 59 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); 
});