img标签不能在src中使用相对路径 [英] img tag not working with relative path in src
问题描述
这是行不通的:
< img src =../ assets / images / image.jpgalt =替代文字>
但这是正常的:
< img src =http://localhost/abc/def/geh/assets/images/image.jpgalt =替代文字>
在我的场景中,我无法使用绝对路径。
../ assets / images / image.jpg - 这意味着
- '../'从我现在的位置上去一个目录
- 找到'assets /'
- 找到'images'文件夹
- 找到'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
- '../' go up one directory from where I am now
- find the 'assets/' folder
- find the 'images' folder
- 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屋!