【问题标题】:html base tag not working correctlyhtml 基本标签无法正常工作
【发布时间】:2013-06-13 03:40:54
【问题描述】:

我在 head 中使用了 html 基本标签,但效果不佳。

这是我的文件夹结构。

- BaseWebsite (Folder)
- - Root (Folder)
- - default.html
- - css (Folder)
- - js (Folder)
- - image (Folder)

在 IIS 中,我创建了一个指向 BaseWebsite 的新网站并将 Root 设为应用程序。

这是我在default.html中的代码

<html>
<head>

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

</head>
<body>
<img src="image/site.jpg" />
.
.
.
</body>
</html>

当我运行页面时,css 与 href="Root/css/site.css" 一起存在,但 js 和图像丢失,而 src 以 js/site.jsimage/site.jpg 开头

所以我把它放在&lt;head&gt;之后

<base href="/Root/" />

现在,js 和图像都显示了,但 css 没有显示,因为 href 变为 href="/Root/Root/css/site.css"

这是正常的还是我该如何解决?我可以通过将css的href更改为/Root/css/site.css来解决它,但是有很多地方需要改变。

【问题讨论】:

  • 在 html 中结束 标签
  • 感谢您指出这一点。 :)
  • 如果你使用&lt;base href="/Root/" /&gt;作为基础,你不应该使用../css/作为css文件夹的相对路径吗?
  • 我的问题是如果js和image都工作了,为什么css不工作?

标签: html base-path


【解决方案1】:

我不知道 ISS 的行为如何,但请尝试使用点:

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

希望对你有帮助

简要说明: "./" 从 default.html 所在的位置开始。

这里长解释: What does "./" (dot slash) refer to in terms of an HTML file path location?

【讨论】:

  • 是的。我知道它可能会奏效。但我的问题是我无法更新所有 css 链接,因为它们遍布各处..
【解决方案2】:

base element 中,href 属性必须是绝对 URL,例如 http://www.example.com/foo/bar/。以 /(或 .)开头的 URL 是相对的。

您需要修复绝对 URL 或使用其他技术,例如服务器端编程工具或预处理器。

【讨论】:

  • 我认为这是不正确的。基本 URL 可以是相对的。见specMDN
猜你喜欢
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多