Javascript 获取a的绝对路径<;img/>;
使用Javascript,是否有标准方法获取图像的绝对路径Javascript 获取a的绝对路径<;img/>;,javascript,url,image,absolute-path,Javascript,Url,Image,Absolute Path,使用Javascript,是否有标准方法获取图像的绝对路径img.getAttribute(“src”)仅返回HTML中声明的src属性。只需执行.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" 相对源路径 f
img.getAttribute(“src”)
仅返回HTML中声明的src
属性。只需执行.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"
相对源路径
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;
}
为了澄清-
.src
实际上将相对路径转换为绝对路径。示例:var a=newimage();a、 src='/a/./b';警报(a.src)
将正确解析。谢谢,这很明显:-)