[英]Do I need “./” and “../” on HTML files paths?
I'm using relative URL in attributes like href
and src
. 我在
href
和src
等属性中使用相对URL。 Do I really need to use ./
and ../
before the paths to correctly establish the link? 在路径正确建立链接之前,我真的需要使用
./
和../
吗?
When I'm done coding index.html, I copy the code and use it on others .html files of my project. 完成index.html的编码后,我将代码复制并在项目的其他.html文件中使用。 I find it frustrating having to correct every path then.
我发现必须纠正每条路径而感到沮丧。
My projects folder structure is like: 我的项目文件夹结构如下:
website-project folder
index.html
css folder
style.css
img folder
image.png
about folder
about.html
If I use index.html
code to start about.html
I need to change every path begging from ./
to ../
. 如果我使用
index.html
代码启动about.html
,则需要将乞求的每个路径都从./
更改为../
。 Using "Find & Replace" tool makes it easier but still it would be nicer if I could link to paths based on the root folder and not the present folder. 使用“查找和替换”工具可以使它更容易,但是如果我可以基于根文件夹而不是当前文件夹链接到路径,那还是更好。 Ex:
例如:
Using 使用
href="website-project/img/image.png"
inside about.html
would link to website-project/img/image.png
instead of website-project/about/img/image.png
which leads to an error. 在
about.html
内部将链接到website-project/img/image.png
而不是website-project/about/img/image.png
,这会导致错误。
UPDATE JULY 07 2016 2016年7月7日更新
Inside index.html
page if I make a <a>
with href="/about/about.html
instead of linking to: 在
index.html
页面中,如果我使用href="/about/about.html
而不是链接到<a>
进行链接:
file:///home/guizo/Documents/website-project/about.html
it links to: 它链接到:
file:///about/about.html
The same goes on for the others links: 其他链接也是如此:
Try using 尝试使用
href="/img/image.png"
/
refers to your root directory. /
指您的根目录。
EDIT: The above will not work for local static websites. 编辑:以上不适用于本地静态网站。 In these cases, you should try using the
<BASE href />
tag which tags links on to a fixed base address. 在这些情况下,您应该尝试使用
<BASE href />
标记,该标记链接到固定的基地址。 It can be used as follows: 可以如下使用:
For index.html
: 对于
index.html
:
<html>
<head>
<title>index</title>
<BASE href="file:///home/guizo/Documents/website-project/" />
</head>
<body>
<a href="about/about.html">Go to about</a>
</body>
</html>
For about.html
: 对于
about.html
:
<html>
<head>
<title>About</title>
<BASE href="file:///home/guizo/Documents/website-project/" />
</head>
<body>
<a href="index.html">Go to index</a>
</body>
</html>
try this '/img/image.png'. 试试这个“ /img/image.png”。 / represents root of the site.
/代表网站的根目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.