gpt4 book ai didi

javascript - Script src 和 Link rel 中的相对路径一直解析为根 URL

转载 作者:行者123 更新时间:2023-11-29 16:47:39 26 4
gpt4 key购买 nike

我有一份文件位于 http://localhost:8081/develop .

index.html , 位于 http://localhost:8081/develop/index.html包含以下内容:

<html>
<head>
...
<link rel="stylesheet" href="./styles.min.css">
<script src="./bower_components/jquery/dist/jquery.js"></script>
<script src="./bower_components/moment/min/moment-with-locales.js"></script>
<script src="./bower_components/elessar/dist/elessar.js"></script>
<script src="./bower_components/EventSource/eventsource.js"></script>
...
</head>
<body>
...
</body>
</html>

所有文件都以 ./ 开头,告诉浏览器它应该在当前目录中搜索它们,例如:<script src="./bower_components/jquery/dist/jquery.js"></script>应该解析为 http://localhost:8081/develop/bower_components/jquery/dist/jquery.js .这很明显。

但事实并非如此。相反,它们被解析为 http://localhost:8081/bower_components/jquery/dist/jquery.js ,在控制台中抛出大量错误。

enter image description here

如果不使用 ./ 也会发生同样的事情在文件的开头,我会删除第一个斜杠:<script src="bower_components/jquery/dist/jquery.js"></script>也解析为 http://localhost:8081/bower_components/jquery/dist/jquery.js .

这感觉像是一个非常基本的问题,但我确实需要这些文件具有相对路径并且这些路径按预期工作。那他们为什么不呢?

最佳答案

原因可以是标签<base href="/">在头部。

关于javascript - Script src 和 Link rel 中的相对路径一直解析为根 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39531741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com