在站点根文件夹内的文件夹中定义 HTML 的根

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17371785/
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 10:35:06  来源:igfitidea点击:

Defining root of HTML in a folder within the site root folder

html

提问by maxmitch

I want to have a new folder containing a set of new html files. All the images inside it are in the format src="image.png"and image.pngis located in the root folder. But when you put the HTML file in a new folder it can't find the image. You have to have it in the format src="../root folder/folder/image.png"to make it work. Which would mean a lot of pasting. I have tried putting image.pnginside the folder but no change.

我想要一个包含一组新 html 文件的新文件夹。里面的所有图片都是格式的src="image.png"image.png位于根文件夹中。但是当你把 HTML 文件放在一个新文件夹中时,它找不到图像。你必须让它的格式src="../root folder/folder/image.png"让它工作。这将意味着很多粘贴。我试过把image.png文件夹放在里面,但没有改变。

回答by j08691

Use the <base>element.It allows you to specify a URL for all relative URLs in a page.

使用<base>元素。它允许您为页面中的所有相对 URL 指定一个 URL。

For example

例如

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>This is an example for the <base> element</title>
        <base href="http://www.example.com/news/index.html">
    </head>
    <body>
        <p>Visit the <a href="archives.html">archives</a>.</p>
    </body>
</html>

The link in the this example would be a link to "http://www.example.com/news/archives.html".

本示例中的链接将是指向“ http://www.example.com/news/archives.html”的链接。

For you, the base URL may be something as simple as <base href="http://www.yoursite.com/">. This would make images specificed as src="image.png"resolve as "http://www.yoursite.com/image.png"

对您来说,基本 URL 可能像<base href="http://www.yoursite.com/">. 这将使图像指定为src="image.png"解析为"http://www.yoursite.com/image.png"

See also https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

另见https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base

回答by Michael Banzon

You need to set the basetag. If you add the tag in your page <head>like this:

您需要设置base标签。如果您<head>像这样在页面中添加标签:

<base href="http://yoursite.tld/folder/">

<base href="http://yoursite.tld/folder/">

it should display images and with sources relative to this base path.

它应该显示图像和相对于这个基本路径的来源。