在HTML中声明根目录时的差异 [英] Differences in declaring your root directory in HTML

查看:123
本文介绍了在HTML中声明根目录时的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很长时间以来一直困扰着我,所以我需要问这个问题。



/ ./ 之间的区别是什么? ,它只是服务器设置?



例如,如果我们在根目录中查找images文件夹,我们会写:



< img src =/ images>



然而,另一个服务器将只接受它,如下所示:



< img src =./ images> p>

这不是一个大问题,但在理想的世界中,我想我可以相对容易地将我的网站转移到另一台服务器,而无需更新这些小细节。

当然,我可以在PHP中声明它,并将其设置为一次配置文件,但它确实在扰乱我。为什么有两种声明根的方法?

解决方案

为了引用相对于当前页面的文件,你可以直接写一个没有前缀的路径,或者你可以直接关于它的前缀是 ./ 。要使用相对于站点根目录的路径访问文件,请在路径前加上 /



摘要



/ 绝对路径(从根目录到资源的完整路径)

./ 当前目录的相对路径(等于未指定任何文件夹前缀)

../ 相对路径从父目录开始

../../ 父母亲的相对路径

示例


$ b

 当前URL资源路径解析为

/ pages / home .html ./picture.jpg /pages/picture.jpg
/pages/home.html ../img/picture.jpg /img/picture.jpg
/pages/about/home.html / img / picture.jpg /img/picture.jpg
/pages/about/home.html img / picture.jpg /pages/about/img/picture.jpg
/home.html img / picture。 jpg /img/picture.jpg


This has been bugging me for a long time so I need to ask the question.

What is the difference between / and ./, is it just down to server settings?

For example, if we were looking for an images folder in the root directory we would write:

<img src="/images">

However, another server will only accept it as follows:

<img src="./images">

It's not a biggie, but in an ideal world I'd like to think I could transfer my sites to another server relatively easily without having to update minor details like these.

Of course, I can declare it in PHP and set it once in a config file, but it really is bugging me. Why is there two methods for declaring the root?

解决方案

To reference files relative to the current page, you can either write the path plainly without a prefix, or you can be explicit about it by prefixing with ./. To reach files with paths relative to the root of your site, you prefix the path with /.

Summary

/ absolute path (full path to resource from root directory)
./ relative path from current directory (equal to not specifying any folder prefix)
../ relative path from parent directory
../../ relative path from parent of parent

Examples

Current URL               Resource path          Resolves to

/pages/home.html          ./picture.jpg          /pages/picture.jpg
/pages/home.html          ../img/picture.jpg     /img/picture.jpg
/pages/about/home.html    /img/picture.jpg       /img/picture.jpg
/pages/about/home.html    img/picture.jpg        /pages/about/img/picture.jpg
/home.html                img/picture.jpg        /img/picture.jpg

这篇关于在HTML中声明根目录时的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆