Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取a的绝对路径<;img/>;_Javascript_Url_Image_Absolute Path - Fatal编程技术网

Javascript 获取a的绝对路径<;img/>;

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

使用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"

相对源路径

  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)
将正确解析。谢谢,这很明显:-)