使用jquery编辑图像src

时间:2013-01-12 13:45:03

标签: javascript jquery

我想编辑所有图像的src属性。我实际上想从所有图像src中删除删除“\ test”。我在jquery中尝试过替换和src.replace,但它没有用。请帮忙怎么做。

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

3 个答案:

答案 0 :(得分: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);
});

答案 1 :(得分:1)

试试这个......

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

答案 2 :(得分:0)

改为使用$(this)

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