img标签不能在src中使用相对路径 [英] img tag not working with relative path in src

查看:1202
本文介绍了img标签不能在src中使用相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是行不通的:

 < img src =../ assets / images / image.jpgalt =替代文字> 

但这是正常的:

 < img src =http://localhost/abc/def/geh/assets/images/image.jpgalt =替代文字> 

在我的场景中,我无法使用绝对路径。

解决方案

../ assets / images / image.jpg - 这意味着


  1. '../'从我现在的位置上去一个目录

  2. 找到'assets /'
  3. 找到'images'文件夹

  4. 找到'image.jpg'文件。

只有当您的网页位于中的子文件夹中时,该相对链接才有效
$ b

http:// localhost / abc / def / geh /

你的网页确实是

localhost / asdf / asdf / asdf / asdf / index.php

(这看起来很荒谬)到达资产文件夹相对而言,你将不得不一直到根。



'../../../。 ./abc/deh/geh/assets/images/image.jpg;

或者,您可以在head标签中使用基本标签来使URL在实际的src中属性更友好。


This is not working:

<img src="../assets/images/image.jpg" alt="Alternative Text">

But this is working:

<img src="http://localhost/abc/def/geh/assets/images/image.jpg" alt="Alternative Text">

In my scenario, I just cannot work with absolute path. I have to use relative path.

解决方案

"../assets/images/image.jpg" -This means

  1. '../' go up one directory from where I am now
  2. find the 'assets/' folder
  3. find the 'images' folder
  4. find the 'image.jpg' file.

That relative link will only work if your page is in a subfolder in

"http://localhost/abc/def/geh/"

If the location of your page really is

"localhost/asdf/asdf/asdf/asdf/index.php"

(which seems ridiculous) to get to the assets folder relatively you would have to go all the way to the root.

'../../../../abc/deh/geh/assets/images/image.jpg;

Alternatively you could use a base tag in your head tag to make the URL in the actual src attribute more friendly.

这篇关于img标签不能在src中使用相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆