为脚本链接指定 HTML 目录的根路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5713295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:06:19  来源:igfitidea点击:

Specify a Root Path of your HTML directory for script links?

htmlpath

提问by Gazow

I'm writing a template for dreamweaver, and don't want to change the scripts for subfolder pages.

我正在为 Dreamweaver 编写模板,并且不想更改子文件夹页面的脚本。

Is there a way to make the path relative to the root directory?

有没有办法使路径相对于根目录?

for example:

例如:

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

Instead of **root**above, I want a default path there. Is there any way to do anything like this?

而不是**root**上面,我想要一个默认路径。有没有办法做这样的事情?

回答by Quentin

To be relative to the root directory, just start the URI with a /

要相对于根目录,只需以 /

<link type="text/css" rel="stylesheet" href="/style.css" />
<script src="/script.js" type="text/javascript"></script>

回答by Alexander Jank

I recommend using the HTML <base>element:

我建议使用 HTML<base>元素

<head>
    <base href="http://www.example.com/default/">
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
</head>

In this example, the stylesheet is located in http://www.example.com/default/style.css, the script in http://www.example.com/default/script.js. The advantage of <base>over /is that it is more flexible. Your whole website can be located in a subdirectory of a domain, and you can easily alter the default directory of your website.

在此示例中,样式表位于 中http://www.example.com/default/style.css,脚本位于 中http://www.example.com/default/script.js<base>over的优点/是更加灵活。您的整个网站可以位于域的子目录中,您可以轻松更改网站的默认目录。

回答by MistyDawn

/means the root of the current drive;

/表示当前驱动器的根;

./means the current directory;

./表示当前目录;

../means the parent of the current directory.

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

回答by Lee Kowalkowski

Just start it with a slash? This means root. As long as you're testing on a web server (e.g. localhost) and not a file system (e.g. C:) then that should be all you need to do.

只用斜线开头?这意味着根。只要您在 Web 服务器(例如 localhost)而不是文件系统(例如 C:)上进行测试,那么这就是您需要做的全部工作。

回答by TriangleGM

This is oddly confusing to me. I know it shouldn't be. To check my understanding, I'd like to use a family relations model to compare. Assuming "You" is the current webpage, is the following correct?

这对我来说很奇怪。我知道这不应该。为了检查我的理解,我想使用家庭关系模型进行比较。假设“您”是当前网页,以下正确吗?

<img src="picture.jpg">            In your folder with you, like a sibling
<img src="images/picture.jpg">     In your child's folder, under you
<img src="../picture.jpg">         In your parent's folder, above you
<img src="/images/picture.jpg">    In your cousin's folder

So, up to parent, over to sibling, down to their child = your cousin, named "images".

所以,上至父母,上至兄弟姐妹,下至他们的孩子 = 您的堂兄,名为“图像”。

回答by Agnostikguy

use two periods before / , example: "../style.css"

在 / 之前使用两个句点,例如:“../style.css”

回答by Ahmed Khaja

You can use ResolveUrl

您可以使用ResolveUrl

<link type="text/css" rel="stylesheet" href="<%=Page.ResolveUrl("~/Content/table-sorter.css")%>" />