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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/296873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:44:38  来源:igfitidea点击:

Basic HTML - how to set relative path to current folder?

htmlpath

提问by serg

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 folderanywhere? (And using only HTML.)

是否可以在此页面上创建指向而http://example.com/folder/无需指定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.

更新:结果证明./只在非严格的文档类型模式下有效,而.在两种模式下都有效,所以在我看来它仍然是一个更好的答案:)谢谢大家。

回答by MrChrister

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

只是点正在工作。文档类型有所不同,但有时 ./ 也很好。

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

回答by Mark Giblin

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.extas 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.htmlin the document yummy.html, then you would be addressing the path /cheese/butter/spread.html, as far as the server was concerned.

../从当前文件夹向上一级处理路径。如果您在路径中/cheese/crackers/yummy.html,并且../butter/spread.html文档中要求您的链接代码yummy.html,那么就/cheese/butter/spread.html服务器而言,您将寻址路径。

/will always address the rootof the site.

/将始终寻址站点的根目录

回答by Vincent Ramdhanie

You can use

您可以使用

 ../

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

意思是上一层。如果在page.html所在的文件夹中有一个名为page2.html的页面,则相对路径为:

 page2.html.

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

如果您的page2.html与文件夹处于同一级别,则路径为:

  ../page2.html

回答by Bullines

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

回答by bdukes

Both of the below seem to work

以下两个似乎都有效

./

./

.

.

回答by JayPex

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

回答by Steve Tranby

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