您可以设置本地文档根目录吗?或者在哪里?

发布于 2024-07-05 05:42:55 字数 227 浏览 6 评论 0原文

当从硬盘驱动器在浏览器中打开文件时,文档根目录在哪里? 为了说明这一点,给定以下 HTML 代码,如果从本地计算机 (file:///) 打开页面,那么浏览器应该将 css 文件放在哪里找到它?

<link href="/temp/test.css" rel="stylesheet" type="text/css" />

When opening a file from your hard drive into your browser, where is the document root? To illustrate, given the following HTML code, if the page is opened from the local machine (file:///) then where should the css file be for the browser to find it?

<link href="/temp/test.css" rel="stylesheet" type="text/css" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

御守 2024-07-12 05:42:56

Eric,文档根目录是您的文件所在的文件夹,无论它在哪里。

Eric, the document root is the folder in which your file is, wherever it may be.

蓝海 2024-07-12 05:42:56

就本地静态 html 而言,除非您指定它,否则大多数浏览器都会将您正在查看的 html 文件的位置作为根。 因此,放入其中的任何 css 都只能通过其名称来引用。

获取 css 文件的正确引用的懒惰方法是在浏览器中打开它。 然后只需获取您在那里看到的网址 - 类似:

file:///blah/test.css

and copy that into your stylesheet link on your html:

<link href="file:///blah/test.css" rel="stylesheet" type="text/css">

或者您可以只获取 html 文件的 url 并修改它以引用样式表。

然后您的本地页面应该可以使用本地样式表正常加载。

As far as local, static html goes, unless you specify it, most browsers will take the location of the html file you are viewing as the root. So any css put in there can just be referenced by it's name only.

The lazy way to get the correct reference for your css file is to open it in your browser. Then just grab the url that you see there - something like:

file:///blah/test.css

and copy that into your stylesheet link on your html:

<link href="file:///blah/test.css" rel="stylesheet" type="text/css">

Either that or you can just take the url for the html file and amend it to refer to the stylesheet.

Then your local page should load fine with the local stylesheet.

暖伴 2024-07-12 05:42:56

如果您有兴趣设置文档根目录,您可能会考虑在您的计算机上安装一个 Web 服务器,或者,如果您已经有一个(如 Apache 或 IIS),请将您正在开发的项目存储在该服务器(Apache 中的 htdocs,IIS 中不完全确定)。 如果您宁愿将文件保留在原处,您可以设置虚拟主机,甚至将它们映射到可以在浏览器中输入的地址(例如,我有一个解析为 Web 根目录的 local.mrwarshaw.com 地址我的个人网站的开发文件夹)。

如果您使用的是 Windows 并且不想自行设置服务器,您可以获取 XAMPP 或 WAMPP 之类的软件包,但请记住,它们带有 PHP 和 MySQL 的额外重量。 不过,如果您有足够的空间,它们对于您的机器来说是一个非常简单的嵌入式开发环境。

If you're interested in setting the document root, you might look at getting a web server installed on your machine, or, if you already have one (like Apache or IIS), storing your project-in-development in the web root of that server (htdocs in Apache, not entirely sure in IIS). If you'd rather leave your files where they are, you can set up virtual hosts and even map them to addresses that you can type into your browser (for example, I have a local.mrwarshaw.com address that resolves to the web root of my personal site's development folder).

If you're on Windows and don't want to mess around with setting up a server on your own, you could get a package like XAMPP or WAMPP, though bear in mind that those carry the extra weight of PHP and MySQL with them. Still, if you've got the space, they're a pretty easy drop-in development environment for your machine.

鸩远一方 2024-07-12 05:42:55

可以,但可能不想在文件头中按文件设置文档根目录:

<base href="my-root">

You can, but probably don't want to, set the document root on a per-file basis in the head of your file:

<base href="my-root">
暮倦 2024-07-12 05:42:55

这取决于您使用的浏览器,但例如 Internet Explorer 会将您带到硬盘的根目录(例如 C:/),而 Firefox 等浏览器则不执行任何操作。

It depends on what browser you use, but Internet Explorer, for example, would take you to the root directory of your harddrive (eg. C:/), while browsers such as Firefox does nothing.

倾`听者〃 2024-07-12 05:42:55

在 Mac 上,文档根目录是您在双击桌面上的主硬盘驱动器图标后出现的窗口中看到的内容。 临时文件夹需要位于其中,以便浏览器找到您在代码中编写的 CSS 文件。

其实,你也可以这样写代码:

<link href="file:///temp/test.css" rel="stylesheet" type="text/css" />

On a Mac, the document root is what you see in the window that appears after you double click on the main hard drive icon on your desktop. The temp folder needs to be in there for a browser to find the CSS file as you have it written in your code.

Actually, you could also write the code like this:

<link href="file:///temp/test.css" rel="stylesheet" type="text/css" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文