基本HTML -如何设置当前文件夹的相对路径?

时间:2022-11-28 22:55:48

Lets say I am currently at: http://example.com/folder/page.html

假设我现在在:http://example.com/folder/page.html

Is it possible to create a relative link on this page that points to http://example.com/folder/ without specifying folder anywhere? (And using only HTML.)

是否可以在此页面上创建指向http://example.com/folder/的相对链接,而无需在任何地方指定文件夹?(只使用HTML。)

UPDATE: As it turned out ./ works only in non-strict doctype mode, while . works in both modes, so it is still a better answer in my opinion :) Thanks everybody.

更新:结果是。/只能在非严格的doctype模式下工作。这两种模式都可以,所以在我看来这仍然是一个更好的答案:)谢谢大家。

7 个解决方案

#1


72  

Just dot is working. The doctype makes a difference however as sometimes the ./ is fine as well.

只是点工作。doctype起了很大的作用,但是有时。/也可以。

<a href=".">Link to this folder</a>

#2


33  

For anyone who has found this thread, addressing relative paths has always created arguments over what is correct or not.

对于任何发现了这个线程的人来说,寻址相对路径总是会引发关于什么是正确的或错误的争论。

Depending on where you use the path to be addressed, it will depend on how you address the path.

取决于您使用要寻址的路径的位置,它将取决于您如何寻址路径。

Generally :

一般来说:

. and ./ do the same thing, however you wouldn't use . with a file name. Otherwise you will have the browser requesting .filename.ext as a file from the server. The proper method would be ./filename.ext.

。和。/做同样的事情,但是你不会用。文件名称。否则,浏览器将请求.filename。ext作为服务器文件。正确的方法应该是./filename.ext。

../ addresses the path up one level from the current folder. If you were in the path /cheese/crackers/yummy.html, and your link code asked for ../butter/spread.html in the document yummy.html, then you would be addressing the path /cheese/butter/spread.html, as far as the server was concerned.

. ./处理从当前文件夹向上一级的路径。如果你在路上/奶酪/饼干/美味。html,你的链接代码要求。html在文档yummy。html,然后你会找到路径/奶酪/黄油/面包。html,就服务器而言。

/ will always address the root of the site.

/将始终位于站点的根。

#3


10  

<html>
    <head>
        <title>Page</title>
    </head>
    <body>
       <a href="./">Folder directory</a> 
    </body>
</html>

#4


8  

You can use

您可以使用

 ../

to mean up one level. If you have a page called page2.html in the same folder as page.html then the relative path is:

表示上一层。如果你有一个页面叫做page2。html在与页面相同的文件夹中。那么相对路径是:

 page2.html.

If you have page2.html at the same level with folder then the path is:

如果你有所以page2。与文件夹相同级别的html,路径为:

  ../page2.html

#5


3  

Both of the below seem to work

以下两种方法似乎都有效

./

。/

.

#6


1  

<a href="./">Folder</a>

#7


1  

The top answer is not clear enough. here is what worked for me: The correct format should look like this if you want to point to the actual file:

最上面的答案还不够清楚。以下是对我有用的:如果你想要指向实际的文件,正确的格式应该是这样的:

 <a href="./page.html">

This will have you point to that file within the same folder if you are on the page http://example.com/folder/index.html

如果您在http://example.com/folder/index.html页面上,那么您将指向同一个文件夹中的文件

#1


72  

Just dot is working. The doctype makes a difference however as sometimes the ./ is fine as well.

只是点工作。doctype起了很大的作用,但是有时。/也可以。

<a href=".">Link to this folder</a>

#2


33  

For anyone who has found this thread, addressing relative paths has always created arguments over what is correct or not.

对于任何发现了这个线程的人来说,寻址相对路径总是会引发关于什么是正确的或错误的争论。

Depending on where you use the path to be addressed, it will depend on how you address the path.

取决于您使用要寻址的路径的位置,它将取决于您如何寻址路径。

Generally :

一般来说:

. and ./ do the same thing, however you wouldn't use . with a file name. Otherwise you will have the browser requesting .filename.ext as a file from the server. The proper method would be ./filename.ext.

。和。/做同样的事情,但是你不会用。文件名称。否则,浏览器将请求.filename。ext作为服务器文件。正确的方法应该是./filename.ext。

../ addresses the path up one level from the current folder. If you were in the path /cheese/crackers/yummy.html, and your link code asked for ../butter/spread.html in the document yummy.html, then you would be addressing the path /cheese/butter/spread.html, as far as the server was concerned.

. ./处理从当前文件夹向上一级的路径。如果你在路上/奶酪/饼干/美味。html,你的链接代码要求。html在文档yummy。html,然后你会找到路径/奶酪/黄油/面包。html,就服务器而言。

/ will always address the root of the site.

/将始终位于站点的根。

#3


10  

<html>
    <head>
        <title>Page</title>
    </head>
    <body>
       <a href="./">Folder directory</a> 
    </body>
</html>

#4


8  

You can use

您可以使用

 ../

to mean up one level. If you have a page called page2.html in the same folder as page.html then the relative path is:

表示上一层。如果你有一个页面叫做page2。html在与页面相同的文件夹中。那么相对路径是:

 page2.html.

If you have page2.html at the same level with folder then the path is:

如果你有所以page2。与文件夹相同级别的html,路径为:

  ../page2.html

#5


3  

Both of the below seem to work

以下两种方法似乎都有效

./

。/

.

#6


1  

<a href="./">Folder</a>

#7


1  

The top answer is not clear enough. here is what worked for me: The correct format should look like this if you want to point to the actual file:

最上面的答案还不够清楚。以下是对我有用的:如果你想要指向实际的文件,正确的格式应该是这样的:

 <a href="./page.html">

This will have you point to that file within the same folder if you are on the page http://example.com/folder/index.html

如果您在http://example.com/folder/index.html页面上,那么您将指向同一个文件夹中的文件