Javascript 将绝对路径转换为相对路径
通过javascript或jquery将绝对路径转换为相对路径的最佳方法是什么Javascript 将绝对路径转换为相对路径,javascript,jquery,relative-path,absolute-path,Javascript,Jquery,Relative Path,Absolute Path,通过javascript或jquery将绝对路径转换为相对路径的最佳方法是什么 例如:考虑如下: "http://localhost:2011/Content/Images/Product/Large/3.jpg" 到 只需使用window.location.pathname使用元素包含位置属性的技巧 $("<a href='http://localhost:2011/Content/Images/Product/Large/3.jpg'>") .prop("pathname
例如:考虑如下:
"http://localhost:2011/Content/Images/Product/Large/3.jpg"
到
只需使用
window.location.pathname
使用
元素包含位置属性的技巧
$("<a href='http://localhost:2011/Content/Images/Product/Large/3.jpg'>")
.prop("pathname");
$(“”)
.prop(“路径名”);
没有javascript或jquery自然提供的方法?@any:只有2-3行代码。不需要库函数:获取主机部分的长度,确保URL从主机部分开始,创建从host-part.length.property开始的子字符串!你能参考任何解释“路径名”属性的文档吗?我们还可以依靠什么样的道具?@abernier:不过,它确实说明了它是HTML5。我不知道跨浏览器支持。这不是相对路径。要想成为相对的,它必须是相对的。例如,如果页面为http://localhost:2011/Content/Pages/Page.html
则相对路径为。/Images/Product/Large/3.jpg
$("<a href='http://localhost:2011/Content/Images/Product/Large/3.jpg'>")
.prop("pathname");