[英]Do I need “./” and “../” on HTML files paths?
我在href
和src
等屬性中使用相對URL。 在路徑正確建立鏈接之前,我真的需要使用./
和../
嗎?
完成index.html的編碼后,我將代碼復制並在項目的其他.html文件中使用。 我發現必須糾正每條路徑而感到沮喪。
我的項目文件夾結構如下:
website-project folder
index.html
css folder
style.css
img folder
image.png
about folder
about.html
如果我使用index.html
代碼啟動about.html
,則需要將乞求的每個路徑都從./
更改為../
。 使用“查找和替換”工具可以使它更容易,但是如果我可以基於根文件夾而不是當前文件夾鏈接到路徑,那還是更好。 例如:
使用
href="website-project/img/image.png"
在about.html
內部將鏈接到website-project/img/image.png
而不是website-project/about/img/image.png
,這會導致錯誤。
2016年7月7日更新
在index.html
頁面中,如果我使用href="/about/about.html
而不是鏈接到<a>
進行鏈接:
file:///home/guizo/Documents/website-project/about.html
它鏈接到:
file:///about/about.html
其他鏈接也是如此:
嘗試使用
href="/img/image.png"
/
指您的根目錄。
編輯:以上不適用於本地靜態網站。 在這些情況下,您應該嘗試使用<BASE href />
標記,該標記鏈接到固定的基地址。 可以如下使用:
對於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>
對於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>
試試這個“ /img/image.png”。 /代表網站的根目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.