javascript用完整的img url替换img src

时间:2013-10-21 10:52:54

标签: javascript php html image

好的,所以我有一个JSON提要,出于某种原因提供了一些图像,如

img src=\"..\/..\/upload\/Politcis\/Tony-Abbott.jpg\" alt=\"Tony-Abbott.jpg\" width=\"980\" height=\"653\" align=\"left\" \/>

完整的JSON字符串http://media.queerdio.com/mobileDevice/?uri=loadstory/high-court-to-decide-on-act-marriage-law

我想知道如何使用javascript将img src替换为完整的img src。

对于这个图像,根据API,完整的img src应该是。

http://media.queerdio.com/news/upload/Politcis/Tony-Abbott.jpg

我知道如果我必须首先获得src链接,并在上传开始时将其拆分

然后我需要一个var链接到http://media.queerdio.com/news/ 然后我会将上传和src链接的其余部分添加到它,然后替换img src。

所以我理解了我想要做的基本知识,但请注意100%确定如何做我需要的。

1 个答案:

答案 0 :(得分:0)

以下是您的网站已经拥有jQuery的伪代码:

var htmlFromJson; //you json object
var imgHost = 'http://media.queerdio.com/news/';
$(htmlFromJson).find('img').each( //find the img from the JSON str
    function(){
        var newSrc = imgHost + $(this).attr('src'); //access add custom host in img src
        newSrc = fixUrl(newSrc); //remember to replace the ../ & parent folder name
        $(this).attr('src', newSrc); // update the 
    }
);

希望我能帮到你。 PS:如果你在某个地方误解,请告诉我,我试着解决。

相关问题