我想編輯所有圖像的src屬性。我實際上想從所有圖像src中刪除「\ test」。我試過在jQuery中替換和src.replace,但它沒有成功。請幫助如何做到這一點。使用jquery編輯圖像src
$("img").each(function(){
var srcVar = this.attr('src');
this.attr('src', "http://www.abc.gr"+srcVar);
});
我想編輯所有圖像的src屬性。我實際上想從所有圖像src中刪除「\ test」。我試過在jQuery中替換和src.replace,但它沒有成功。請幫助如何做到這一點。使用jquery編輯圖像src
$("img").each(function(){
var srcVar = this.attr('src');
this.attr('src', "http://www.abc.gr"+srcVar);
});
使用$(this)
代替:
$("img").each(function(){
var srcVar = $(this).attr('src').replace("/test", "");;
$(this).attr('src', srcVar);
});
這會幫助你:
$("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);
});
nope無變化:( – james
嘗試了這一點...
$("img").each(function(){
var srcVar = $(this).attr('src');
srcVar = srcVar.replace("/test", "");
$(this).attr('src', srcVar);
});
+ srcVar.split(「/測試「)[1] – mplungjan