還有就是代碼段共享,除去基本的URL here:
function RemoveBaseUrl(url) {
/*
* Replace base URL in given string, if it exists, and return the result.
*
* e.g. "http://localhost:8000/api/v1/blah/" becomes "/api/v1/blah/"
* "/api/v1/blah/" stays "/api/v1/blah/"
*/
var baseUrlPattern = /^https?:\/\/[a-z\:0-9.]+/;
var result = "";
var match = baseUrlPattern.exec(url);
if (match != null) {
result = match[0];
}
if (result.length > 0) {
url = url.replace(result, "");
}
return url;
}
你可以用callbac使用.attr()
方法的k個功能:
$('a').attr('href',function(i,o){
return RemoveBaseUrl(o) ;
});
Working Demo
這個怎麼樣的解決方案: [轉換相對路徑絕對使用JavaScript(http://stackoverflow.com/questions/14780350 /轉換相對路徑絕對使用JavaScript) – AlexBerd