使用 Javascript,是否有标准方法来获取图像的绝对路径?img.getAttribute("src")
只返回src
在 HTML 中声明的属性。
问问题
11705 次
2 回答
22
做吧.src
。
$('img')[0].src = '/images/foo.gif'
"/images/foo.gif"
$('img')[0].src
"http://stackoverflow.com/images/foo.gif"
$('img')[0].getAttribute('src')
"/images/foo.gif"
于 2010-08-16T19:19:58.700 回答
0
对于相对源路径
function getImageURI(imagePath) {
if (imagePath.indexOf('http') == 0) {
return imagePath
}
var rootPath = window.location.protocol + "//" + window.location.host + "/";
var path = window.location.pathname;
if (path.indexOf("/") == 0) {
path = path.substring(1);
}
path = path.split("/", 1);
if (path != "") {
rootPath = rootPath + path + "/";
}
return rootPath + imagePath;
}
于 2021-02-27T10:36:00.863 回答