Moo*_*oon 58 html directory reference src subdirectory
让我们说以下是我网站的DIR结构

现在在index.html我可以简单地参考图像
<img src="./images/logo.png">
Run Code Online (Sandbox Code Playgroud)
但是如果我想sub.html在src中引用相同的图像呢?
Mos*_*ses 90
../images/logo.png 将你移回一个文件夹.
../../images/logo.png 将你移回两个文件夹.
/images/logo.png 无论你在哪里/都会带你回到根文件夹.
aul*_*ron 77
<img src="../images/logo.png">
__ ______ ________
| | |
| | |___ 3. Get the file named "logo.png"
| |
| |___ 2. Go inside "images/" subdirectory
|
|
|____ 1. Go one level up
Run Code Online (Sandbox Code Playgroud)
Ber*_*t F 11
相对参考将是
<img src="../images/logo.png">
Run Code Online (Sandbox Code Playgroud)
如果您知道相对于服务器根目录的位置,对于具有复杂嵌套目录层次结构的应用程序而言,这可能是最简单的方法 - 从所有文件夹中它们都是相同的.
例如,如果您的问题中描述的目录树是相对于服务器的根目录,那么index.html和sub_folder/sub.html都将使用:
<img src="/images/logo.png">
Run Code Online (Sandbox Code Playgroud)
如果images文件夹位于foo服务器根目录(例如http://www.example.com/foo)下方的应用程序的根目录中,则index.html(http://www.example.com/foo/index.html)eg和sub_folder/sub.html(http://www.example.com/foo/sub_folder/sub.html)都使用:
<img src="/foo/images/logo.png">
Run Code Online (Sandbox Code Playgroud)