15

使用 Javascript,是否有标准方法来获取图像的绝对路径img.getAttribute("src")只返回src在 HTML 中声明的属性。

4

2 回答 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 回答