为什么开发人员会在每个相对路径的开头放置一个正斜杠? [英] Why would a developer place a forward slash at the start of each relative path?

查看:24
本文介绍了为什么开发人员会在每个相对路径的开头放置一个正斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为朋友检查一些代码,发现构建他的网站的开发人员开始了每一个相关的 srchrefinclude 带有正斜杠 /.

I am examining some code for a friend, and have found that the developer who built his site began each and every relative src, href, and include with a forward slash /.

例如:

src="/assets/js/jquery.js"

我以前从未见过这种情况.所以我的问题是,为什么开发人员要在相对路径的开头放置一个正斜杠 /?

I have never seen this before. So my question is, why would a developer place a forward slash / at the start of a relative path?

推荐答案

这样做是为了根路径(使其成为绝对路径).

It's done in order to root the path (making it an absolute path).

它确保路径不是相对的,而是从站点的根目录读取.

It ensures that the path is not relative but read from the root of the site.

这允许人们移动文件而不必更改指向不同资源的链接.

This allows one to move a file around and not have to change the links to the different resources.

使用您的示例:

src="/assets/js/jquery.js"

如果引用文件在 /pages/admin/main.html(例如)中,使用相对路径,您将使用:

If the referencing file is in /pages/admin/main.html (for example) using relative paths you would use:

src="../../assets/js/jquery.js"

假设您将文件移动到子目录.原始根路径不需要更改,但相对路径需要更改为:

Suppose you move the file to a child directory. No changes would be needed for with the original rooted path, but the relative one would need to change to:

src="../../../assets/js/jquery.js"

这篇关于为什么开发人员会在每个相对路径的开头放置一个正斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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