下面是我的网站的目录结构:
现在在index.html
中,我可以简单地引用如下图像:
<img src="./images/logo.png">
但我想引用来自sub.html
的同一张图片。src
应该是什么
发布于 2010-09-07 08:42:10
您可以使用相对路径引用图像:
<img src="../images/logo.png">
__ ______ ________
| | |
| | |___ 3. Get the file named "logo.png"
| |
| |___ 2. Go inside "images/" subdirectory
|
|
|____ 1. Go one level up
或者您可以使用绝对路径:/
表示这是服务器上的绝对路径,因此如果您的服务器位于https://example.org/,则从任何页面引用/images/logo.png
都将指向https://example.org/images/logo.png
<img src="/images/logo.png">
|______ ________
| | |
| | |___ 3. Get the file named "logo.png"
| |
| |___ 2. Go inside "images/" subdirectory
|
|
|____ 1. Go to the root folder
发布于 2010-09-07 08:42:51
../images/logo.png
将向后移动一个文件夹。
../../images/logo.png
将向后移动两个文件夹。
无论您在/何处,/images/logo.png
都会带您回到根文件夹。
发布于 2010-09-07 08:50:47
相对引用将是
<img src="../images/logo.png">
如果您知道相对于服务器根目录的位置,对于具有复杂嵌套目录层次结构的应用程序来说,这可能是最简单的方法-所有文件夹都是相同的。
例如,如果问题中描述的目录树相对于服务器的根目录,则index.html和sub_folder/sub.html都将使用:
<img src="/images/logo.png">
如果图像文件夹位于服务器根目录(例如http://www.example.com/foo
)下的应用程序根目录(如foo
)中,则index.html (http://www.example.com/foo/index.html
)和sub_ folder /sub.html (http://www.example.com/foo/sub_folder/sub.html
)都使用:
<img src="/foo/images/logo.png">
https://stackoverflow.com/questions/3655059
复制相似问题