js 获取文本中的图片src并替换图片src

我提供一点例子数据
var text = "<p style="background-color:#ffffff;text-indent:0px;color:#333333;">
<img style="width:666px;height:402px;" alt="" src="/admin/kindeditor/attached/image/20140504/20140504080935_30878.jpg" width="673" height="402" /></p><p style="background-color:#ffffff;text-indent:0px;color:#333333;">
<img style="width:666px;height:402px;" alt="" src="/admin/kindeditor/attached/i04/20140504080935_30878.jpg" width="673" height="402" /></p>
";

需要操作的就是获取这段数据中的img的src 然后在src前面添加http://www.nmgjzw.com
说白了就是更改src

第1个回答  推荐于2016-06-23
<script>

(function(){
var imglist=document,getElementsByTagName("img");

for(var i=0;i<imglist.length;i++){

imglist[i].src="http://www.nmgjzw.com"+imglist[i].src;
}

})()
</script>追问

如果这样的话就会替换整个页面的图片了,有没有方法只更改这个内容的img

追答

var text=text.replace("src=\"","src=\"http://www.nmgjzw.com")

本回答被提问者采纳