您可以用一些正则表达式重写它们:
var baseUrl = "http://somesite.com/somepage"
var root = baseUrl.match(/^[^:]+:\/\/[^\/]+\//)[0];
// "http://somesite.com/"
var HTML = "<a href='/testing'>test</a> and <a class='test' href=\"/foo/bar\"> </a>";
HTML.replace(/<a [^>]*href=["']\/([^'"]+)["']/ig, function (whole, url) {
return whole.replace("/"+url, root+url);
});
// "<a href='http://somesite.com/testing'>test</a> and <a class='test' href=\"http://somesite.com/foo/bar\"> </a>"