【问题标题】:Computation of relative path相对路径的计算
【发布时间】:2015-02-24 07:25:17
【问题描述】:

我在理解相对路径概念方面遇到困难,我看到部分代码写成

../../abc/file/images/picutre/down.gif

如何计算相对路径

【问题讨论】:

标签: javascript relative-path


【解决方案1】:

相对路径是相对于工作目录的路径。换句话说,查找文件的起点是工作目录。

相对路径中的“../”表示上一级目录。

假设您从 index.html 页面中引用相对路径 ../../abc/file/images/picutre/down.gif,结构如下:

http://someexampleurl.com/dir1/dir2/index.html

您从 index.html 工作时的工作目录是 /dir2 因此考虑到您要上两层,浏览器希望文件位于:

 http://someexampleurl.com/abc/file/images/picutre/down.gif

【讨论】:

    【解决方案2】:

    如何计算相对路径

    基本上,相对路径是从您所在的目录到您需要包含的文件的“映射”。因此,相对路径是根据您想去的地方计算的。

    例如你有一个结构

    / (document root)
    |--home.php
    |--t.php
    |--common
          |--header.php
          |--footer.php
    |--support
          |--index1.php
    |--privacy
    |     |--index2.php
    

    来自home.php,您需要包括headerfooter。因此,您的家庭代码看起来像

    <?php
    include("common/header.php"); // go one folder down (common) and grab the file header.php
    include("common/footer.php"); // go one folder down (common) and grab the file footer.php
    

    现在假设您在支持 index1.php,并且您需要 header.phpfooter.php。你的代码看起来像

    <?php
    include("../common/header.php"); // go one folder up (common) and grab the file header.php
    include("../common/footer.php"); // go one folder up (common) and grab the file footer.php
    

    将文件夹内的文件夹视为级别(级别 1、级别 2 等)

    注意:小心相对路径,因为它们很痛苦。

    【讨论】:

      【解决方案3】:

      它说从当前位置返回两级(父目录)“../../”。

      【讨论】:

        【解决方案4】:

        因此,如果我们在https://example.com/my/path/here 上并且它加载了一个文件../../abc/file/images/picutre/down.gif,那么我们将上升2 个目录,因为2 个../https://example.com/my。然后我们会去/abc/file/images/picutre/down.gif。所以最终目的地是https://example.com/my/abc/file/images/picutre/down.gif

        【讨论】:

          【解决方案5】:
          1. down.gif 存在于同一目录中
          2. / 从根目录开始
          3. ../从当前目录返回一个目录
          4. ../../从当前目录返回两个目录

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-13
            • 1970-01-01
            • 2017-06-15
            • 2011-11-02
            相关资源
            最近更新 更多