javascript 文件无法识别但位于同一文件夹中

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

files not recognised BUT in same folder

javascripthtmlcssanchor

提问by user94628

I have three files in my templates folder wheree there is also my main.html file. However when I load the html file I get a 404 error for these 3 files. This is how I've included them in the head of the html:

我的模板文件夹中有三个文件,其中还有我的 main.html 文件。但是,当我加载 html 文件时,这 3 个文件出现 404 错误。这就是我将它们包含在 html 的头部的方式:

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

They are all located in a templates folder in the same folder as my main.html.

它们都位于与我的 main.html 相同的文件夹中的模板文件夹中。

Why aren't they being recognised?

为什么他们不被认可?

Thanks

谢谢

回答by Evil Closet Monkey

Your local paths are not resolving correctly. Without seeing your directory structure I can't say for sure exactly where things are going wrong -- I'm a little confused as to where main.htmland base.htmlare residing.

您的本地路径未正确解析。没有看到您的目录结构,我无法确定到底哪里出了问题——我对在哪里main.htmlbase.html居住的地方有点困惑。

If main.htmlis the file being loaded in the browser, your scriptsources should be based off that directory. If your template directory (we'll call it 'templates') is a child of the directory where main.htmlresides, you want:

如果main.html是在浏览器中加载的文件,则您的script源应该基于该目录。如果您的模板目录(我们称之为“模板”)是main.html所在目录的子目录,您需要:

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

Alternatively, you could just resolved your entire URL to the appropriate path:

或者,您可以将整个 URL 解析为适当的路径:

<script type="text/javascript" src="http://www.myurl.com/templates/date.js"></script>