根相对 URL 以/
性格,看起来像<a href="/directoryInRoot/fileName.html">link text</a>
.
您发布的链接:<a href="fruits/index.html">Back to Fruits List</a>
链接到位于名为的目录中的 html 文件fruits
,该目录与此链接出现的 html 页面位于同一目录中。
要使其成为根相对 URL,请将其更改为:
<a href="/fruits/index.html">Back to Fruits List</a>
Edited回答OP的评论中的问题:
所以这样做 / 会使其相对于 www.example.com,有没有办法指定根是什么,例如,如果我希望根是 www.example.com/fruits/ 中的 www.example.com/fruits ,该怎么办苹果/apple.html?
是的,在 URL 前面加上href
or src
属性,具有/
将使路径相对于根目录。例如,给定 html 页面www.example.com/fruits/apples.html
, the a
of href="/vegetables/carrots.html"
将链接到该页面www.example.com/vegetables/carrots.html
.
The base tag http://www.w3.org/TR/html401/struct/links.html#h-12.4元素允许您指定该页面的基本 uri(尽管base
标签必须添加到every需要使用特定基础的页面,为此我将简单引用 W3 的示例:
例如,给定以下 BASE 声明和 A 声明:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Our Products</TITLE>
<BASE href="http://www.aviary.com/products/intro.html">
</HEAD>
<BODY>
<P>Have you seen our <A href="../cages/birds.gif">Bird Cages</A>?
</BODY>
</HTML>
相对 URI“../cages/birds.gif”将解析为:
http://www.aviary.com/cages/birds.gif
示例引用自:http://www.w3.org/TR/html401/struct/links.html#h-12.4 http://www.w3.org/TR/html401/struct/links.html#h-12.4.
建议阅读:
- http://www.motive.co.nz/glossary/linking.php http://www.motive.co.nz/glossary/linking.php
- http://www.communitymx.com/content/article.cfm?cid=AEDCC52C4AD230AD http://www.communitymx.com/content/article.cfm?cid=AEDCC52C4AD230AD