如何正确引用HTML中的本地资源?
事实certificate,引用本地资源对于一些人来说可能是一个难题。 我正在寻找本地资源引用的规范答案,以及它们的含义。
拿这些例子来说,这些参考path有什么区别呢?
-
<img src="myfile.png" />
(无斜杠) -
<img src="/myfile.png" />
(带斜杠) -
<img src="folder/myfile.png" />
(没有前导斜杠/在子文件夹中) -
<img src="/folder/myfile.png" />
(用斜杠/在子文件夹中) -
<img src="../folder/myfile.png" />
(带圆点和斜杠/子文件夹)
- 前导斜杠告诉浏览器从根目录开始。
- 如果没有前导斜杠,则从当前目录引用。
- 如果在前导斜杠之前添加两个点,则表示您正在引用当前目录的父项。
采取以下文件夹结构
注意:
- ROOT复选标记是绿色的,
- 第二个选中标记是橙色的,
- 第三个复选标记是紫色的,
- 第四个复选标记是黄色的
现在在index.html.en
文件中,您需要放置以下标记
<p> <span>src="check_mark.png"</span> <img src="check_mark.png" /> <span>I'm purple because I'm referenced from this current directory</span> </p> <p> <span>src="/check_mark.png"</span> <img src="/check_mark.png" /> <span>I'm green because I'm referenced from the ROOT directory</span> </p> <p> <span>src="subfolder/check_mark.png"</span> <img src="subfolder/check_mark.png" /> <span>I'm yellow because I'm referenced from the child of this current directory</span> </p> <p> <span>src="/subfolder/check_mark.png"</span> <img src="/subfolder/check_mark.png" /> <span>I'm orange because I'm referenced from the child of the ROOT directory</span> </p> <p> <span>src="../subfolder/check_mark.png"</span> <img src="../subfolder/check_mark.png" /> <span>I'm purple because I'm referenced from the parent of this current directory</span> </p> <p> <span>src="subfolder/subfolder/check_mark.png"</span> <img src="subfolder/subfolder/check_mark.png" /> <span>I'm [broken] because there is no subfolder two children down from this current directory</span> </p> <p> <span>src="/subfolder/subfolder/check_mark.png"</span> <img src="/subfolder/subfolder/check_mark.png" /> <span>I'm purple because I'm referenced two children down from the ROOT directory</span> </p>
现在,如果您加载位于第二个子文件夹中的index.html.en
文件
http://example.com/subfolder/subfolder/
这将是你的输出