从子文件夹中选取根文件夹的图像

发布于 2024-09-18 08:17:19 字数 269 浏览 15 评论 0原文

假设以下是我网站的目录结构: DIR STRUCTURE

现在在 index.html 中,我可以简单地引用图像,例如:

<img src="./images/logo.png">

但我想引用来自 sub.html 的相同图像。 src 应该是什么?

Let's say following is the directory structure of my website :
DIR STRUCTURE

Now in index.html I can simply refer images like:

<img src="./images/logo.png">

But I want to refer the same image from sub.html. What should be the src?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

葬花如无物 2024-09-25 08:17:19

../images/logo.png 会将您移回一个文件夹。

../../images/logo.png 会将您移回两个文件夹。

/images/logo.png 无论您在哪里,都会带您返回根文件夹。

../images/logo.png will move you back one folder.

../../images/logo.png will move you back two folders.

/images/logo.png will take you back to the root folder no matter where you are/.

雨夜星沙 2024-09-25 08:17:19

您可以使用相对路径引用图像:

<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

You can reference the image using relative path:

<img src="../images/logo.png">
          __ ______ ________
          |    |       |
          |    |       |___ 3. Get the file named "logo.png"
          |    |
          |    |___ 2. Go inside "images/" subdirectory
          | 
          | 
          |____ 1. Go one level up

Or you can use absolute path: / means that this is an absolute path on the server, So if your server is at https://example.org/, referencing /images/logo.png from any page would point to 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
温暖的光 2024-09-25 08:17:19

相对引用是

<img src="../images/logo.png">

如果您知道相对于服务器根目录的位置,这对于具有复杂嵌套目录层次结构的应用程序来说可能是最简单的方法 - 所有文件夹都是相同的。

例如,如果问题中描述的目录树相对于服务器的根目录,则 index.html 和 sub_folder/sub.html 都将使用:

<img src="/images/logo.png">

如果 images 文件夹位于 等应用程序的根目录中foo 在服务器根目录下(例如 http://www.example.com/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">

The relative reference would be

<img src="../images/logo.png">

If you know the location relative to the root of the server, that may be simplest approach for an app with a complex nested directory hierarchy - it would be the same from all folders.

For example, if your directory tree depicted in your question is relative to the root of the server, then index.html and sub_folder/sub.html would both use:

<img src="/images/logo.png">

If the images folder is instead in the root of an application like foo below the server root (e.g. http://www.example.com/foo), then index.html (http://www.example.com/foo/index.html) e.g and sub_folder/sub.html (http://www.example.com/foo/sub_folder/sub.html) both use:

<img src="/foo/images/logo.png">
面如桃花 2024-09-25 08:17:19

您的index.html可以执行src="images/logo.png",而从sub.html您可以执行src="../images/logo.png"

Your index.html can just do src="images/logo.png" and from sub.html you would do src="../images/logo.png"

剑心龙吟 2024-09-25 08:17:19

../ 将带您到目录树上一个文件夹。然后,选择适当的文件夹及其内容。

../images/logo.png

../ takes you one folder up the directory tree. Then, select the appropriate folder and its contents.

../images/logo.png
永不分离 2024-09-25 08:17:19

当您将文件上传到服务器时请小心,有些书籍您的图像不会出现在网页上,并且会出现崩溃的图标,这意味着您的文件路径未正确排列或编码,当您具有以下文件结构时,代码应该像这样
文件结构:
->web(主文件夹)
->images(子文件夹)->logo.png(子文件夹中的图像)上面的代码如下,

 <img src="../images/logo.jpg" alt="image1" width="50px" height="50px">

如果您通过忽略文件结构将文件上传到网络服务器, 请遵循此标准
如果不创建文件夹 web 如果直接上传文件那么你的图像将被破坏你看不到图像,然后更改代码如下

 <img src="images/logo.jpg" alt="image1" width="50px" height="50px">

谢谢->vamshi krishnan

when you upload your files to the server be careful ,some tomes your images will not appear on the web page and a crashed icon will appear that means your file path is not properly arranged or coded when you have the the following file structure the code should be like this
File structure:
->web(main folder)
->images(subfolder)->logo.png(image in the sub folder)the code for the above is below follow this standard

 <img src="../images/logo.jpg" alt="image1" width="50px" height="50px">

if you uploaded your files to the web server by neglecting the file structure
with out creating the folder web if you directly upload the files then your images will be broken you can't see images,then change the code as following

 <img src="images/logo.jpg" alt="image1" width="50px" height="50px">

thank you->vamshi krishnan

唯憾梦倾城 2024-09-25 08:17:19

当您将文件上传到服务器时请小心,有些书籍您的图像不会出现在网页上,并且会出现崩溃的图标,这意味着您的文件路径未正确排列或编码,当您具有以下文件结构时,代码应该像这样的文件结构:->web(主文件夹)->images(子文件夹)->logo.png(子文件夹中的图像)上面的代码如下遵循此标准

<img src="../images/logo.jpg" alt="image1" width="50px" height="50px">

如果您通过忽略文件结构而不创建文件将文件上传到 Web 服务器web文件夹如果你直接上传文件那么你的图像将被破坏你看不到图像,然后更改代码如下

<img src="images/logo.jpg" alt="image1" width="50px" height="50px">

谢谢->vamshi krishnan

when you upload your files to the server be careful ,some tomes your images will not appear on the web page and a crashed icon will appear that means your file path is not properly arranged or coded when you have the the following file structure the code should be like this File structure: ->web(main folder) ->images(subfolder)->logo.png(image in the sub folder)the code for the above is below follow this standard

<img src="../images/logo.jpg" alt="image1" width="50px" height="50px">

if you uploaded your files to the web server by neglecting the file structure with out creating the folder web if you directly upload the files then your images will be broken you can't see images,then change the code as following

<img src="images/logo.jpg" alt="image1" width="50px" height="50px">

thank you->vamshi krishnan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文