我知道…/的意思是沿着一条路走,但是。/到底是什么意思呢?

我最近读了一个教程,它似乎只是指同一个位置的一个文件,所以它是必要的吗?如果它只会这样,我能不用它吗?


是的,./表示当前工作目录。您可以直接通过名称引用文件,而不需要它。

.  = This location
.. = Up a directory

所以。/foo。html就是foo。html。并且它是可选的,但是如果脚本生成了路径,那么它可能具有相关性(即与脚本相关,而不是参考如何工作)。

你说得对,你可以省略它。它只对清晰有用。在那里和不在那里之间没有功能上的区别。

是的。/表示您当前所在的目录。

./是工作文件所在的文件夹:

因此在/index.htm ./中是根目录 但是在/css/style.css ./中是css文件夹。

记住这一点很重要,因为如果你将CSS从/index.htm移动到/ CSS /style.css,路径将会改变。

/表示当前驱动器的根;

./表示当前目录;

../表示当前目录的父目录。

您可以使用以下列表作为快速参考:

   /   = Root directory
   .   = This location
   ..  = Up a directory
   ./  = Current directory
   ../ = Parent of current directory
   ../../ = Two directories backwards

有用的文章: https://css-tricks.com/quick-reminder-about-file-paths/

例如,css文件在名为css的文件夹中,html文件在名为html的文件夹中,这两个文件都在名为XYZ的文件夹中,这意味着我们将html中的css文件称为

<link rel="stylesheet" type="text/css" href="./../CSS/style.css" />

在这里. .移动到HTML 和。表示当前目录XYZ

——按照这个逻辑,你可以引用为:

<link rel="stylesheet" type="text/css" href="CSS/style.css" />

在参考快速参考列表时,具体可以使用以下方法:

\。\根目录+当前目录(盘符)

一个关于路径的快速而简短的概述

绝对路径

http://website.com/assets/image.jpg 如果图像不在你的域-去那里寻找图像


/ / website.com/assets/image.jpg 使用HTTP或HTTPS协议加载的图像


相对路径

(如果映像在同一服务器上,则供内部使用)


image.jpg 图像与文档调用图像的位置相同!


。/ image.jpg 与上面相同,图像在相同的位置,作为文档调用图像!


/资产/ image.jpg 类似于绝对路径,只是省略了协议和域名 从我的根文件夹/开始搜索我的图像,然后到assets/


资产/ image.jpg 此时,assets和文档位于同一位置,因此进入图像的assets


. . /资产/ image.jpg 从文档所在的位置,返回一个文件夹。/进入资产


. . / . . / image.jpg 往后退两个文件夹,这是我的图片!


. . / . . /资产/ image.jpg 返回两个文件夹../而不是进入资产

不要使用。/作为web路径!我在下面解释为什么会这样……

真正的网络路径,你应该总是使用

../siblingfolder/file.html是一个web路径,从你所在的文件夹开始,向上走一个父文件夹(../),下到一个名为“siblingfolder”的新文件夹,并到其中的“file.html”。这是网络世界中的一种相对路径。

Childfolder /file.html是另一种网络路径,从你所在的文件夹开始,一直到“Childfolder”和“file.html”。这也是相对路径。

/subrootfolder/file.html是一个web路径,从你的网站的web根开始,从web根下降到“subrootfolder”作为绝对路径。注意:此路径具有额外的优势,它可以从服务器上的任何文件和文件夹位置工作。

http://somewebsite.com/subrootfolder/file.html是另一个网站路径,它的工作原理与上面的完全相同,但需要你的网站域名在路径中。它仍然可以工作,但非常有限,因为web域是硬编码到路径中。有些人称之为完全限定的网页路径,它有一些用途。web浏览器还将大多数文件路径解析为该地址或该地址的IP版本。

尴尬和很少使用的路径

. 是当前位置或文件上下文的简写,在Linux和Unix中用于在当前目录中执行已编译程序。这就是为什么你在Web开发中很少看到它的使用,除了开源的非windows框架,比如谷歌Angular,它是由那些停留在开源平台上的人编写的。

./ also resolves to the current directory and is atypical in Web but supported as a path in some open source frameworks. Because it resolves the same as no path to the current file directory its not used. Example: ./image.jpg = image.jpg. Even though it is used by software to identify a current folder location, because it is identical to the current software path or file location, it is the same as no path so redundant. Again, this is a relic of Unix operating systems that need path resolutions like this to run executables and resolve paths for security reasons. Its not a typical web path. That is why this syntax is redundant in HTML and web technologies.

//somewebsite.com/folder/folder/file.html this is a form of a fully qualified web URL resolution format. "//" tells the web browser to determine the fully qualified web URL/URI at runtime and concatenate the right http protocol onto the front of your path as so: https://mywebsite.com/folder/folder/file.html. It allows the browser to query one of many web domains and determine the most secure location or protocol to use: Either "http" or "https" as the most favorable and secure prefix. This is rarely used in most internal web paths but likely found in link href attribute paths in newer HTML5 elements and used when adding links to resources that may resolve to unknown dynamic secure socket layer connections at runtime that change.

路径等价物

如果访问该子“文件夹”的文件位于web根目录下,则相对路径与绝对路径相同。这两条路对他们来说都是一样的。

../folder = /folder一个相对路径上升到父文件夹然后下降到"folder"和绝对路径相同,如果文件再次访问这个路径是在web根目录。

./folder = folder两个相对路径都以文件当前文件夹开始,并指向它们下面的子“文件夹”,无论这些文件夹位于何处,因此是相同的。"./"因此是多余的。

./file.html = file.html这两个相对路径都指向当前文件夹,然后指向该文件夹中的“file.html”。这意味着使用"。/"是多余的。

./ = {no path}一个空路径与web世界中的。/相同,所以又冗余了。

./ = /如果文件在web根文件夹下,则只为true。它的意思是“。/"是多余的。

我的网络路径建议

ALWAYS use Absolute Paths ("/myfolder/myfile") whenever possible as they will always work from any file location in the web project, add no confusion to developers, moving files with absolute paths will not change paths, and they are easy to maintain and manage. The only drawback to absolute paths is if you move the files or folders being pathed to new locations (example: You move an image or CSS folder of files to a new location later in the project). That is why I recommend you manage paths via server-side virtual paths, application paths, or server-generated variables so you can change absolute paths dynamically as you move things around.

ALWAYS use Relative Web Paths ("../myfile") as a secondary option. These are a must for CSS file paths, CSS imported files, or font paths within CSS files. They should only be used if your web application is designed with many parallel applications running side-by-side under one domain, and have nested and heavily dependent resource files running inside them that must be separated. In that one case, you will often move these applications as one module deeper into the application making relative pathing more valuable. You will determine paths relative to the source files in that case and not relative to the web domain via absolute paths. As above, I would still manage paths via server-side or virtual application generated paths. This makes paths extremely simple, robust, and easy to dynamically update on the fly via server variables rather than inside scripts and dependencies which constantly evolve.

永远不要使用列出的其他路径,除非被非典型的、第三方的、专有的软件约定所强迫,使用不符合标准的路径解决方案,这会增加混乱和额外的维护。:(