在處理Web頁上一大堆連接的時候,常常被一些相對路徑搞得很迷糊,現(xiàn)在整理一下,當(dāng)作是提醒備忘。其實,很簡單,只是老是不記住。呵
通常我們遇到的相對路徑會有下面三種情況,下面一一來舉例說明。
一、以"/"為首字母的路徑,其完整路徑將會是主機名加上該路徑名
<a href="/article/index.html">article</a>實際指向:http://hostname/article/index.html
二、無斜杠開頭的路徑,其完整路徑將會是當(dāng)前的URL的上一級路徑加上該路徑名
<a href="article/index.html">article</a>,如果當(dāng)前你訪問的頁面地址為http://hostname/book/list.html
則,這個連接將去到http://hostname/book/article/index.html
三、以一到N個.加斜杠開頭的路徑,其實整路徑將會是當(dāng)前的URL的上一級至N級路徑加上該路徑名,第二種情況是這種情況的特殊例子
<a href="./article/index.html">article</a>,如果當(dāng)前你訪問的頁面地址為http://hostname/book/list.html
則,這個連接將去到http://hostname/book/article/index.html
<a href="../article/index.html">article</a>,如果當(dāng)前你訪問的頁面地址為http://hostname/book/list.html
則,這個連接將去到http://hostname/article/index.html