我想编辑所有图像的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