开发者

Strip image src from given html

The html contains images path like

<img src="/testsite/images/abc.jpg" />

I want javascript to conver开发者_运维问答t all images src to be like

<img src="images/abc.jpg"/>

Simply saying I want to remove any domain/folder name before image/abc.jpg.

How can it be done using javascript?


The journey of 1000 miles begins with a single step

var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; ++i) {
  var img = images[i];
  img.src = img.src.replace(/^.*(images/[^/]+)$/, "$1");
}

Now here's the thing: if you serve up a page with <img> elements that have bogus "src" attributes, the browser is going to flail around issuing HTTP "GET" requests to load those URLs. It'd be somewhat nicer if you could arrange for the server not to send the wrong URLs in the first place.


You can do it using replace method Here is a link.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜