Javascript src 中的路径以到达不同目录中的文件

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

Path in src to reach a file in a different directory

javascripthtmlhyperlink

提问by Oni Enzeru

I have a folder called projectsand in it I have a gamefolder and an enginefolder. I have my engine.jsfile inside the enginefolder and I was wondering if I could access it with my game.htmlfile from the other folder like so

我有一个名为的文件夹projects,其中有一个game文件夹和一个engine文件夹。我的engine.js文件在engine文件夹中,我想知道是否可以game.html像这样使用其他文件夹中的文件访问它

 <script type ="text/javascript" src ="\engine/engine.js"/>

Now, obviously the above doesn't work, but what I need to do is go back out of the gamefolder to the projectfolder and then into the enginefolder.

现在,显然上述方法不起作用,但我需要做的是从game文件夹返回到project文件夹,然后再进入engine文件夹。

File tree: projects folder

文件树:项目文件夹

  • projects
    • engine
      • engine.js
    • game
      • game.js
  • 项目
    • 引擎
      • 引擎.js
    • 游戏
      • 游戏.js

What is the proper format for srcto provide users with a link to engine.js?

src为用户提供链接的正确格式是什么engine.js

回答by njbair

Use a double-dot to go back:

使用双点返回:

src="../engine/engine.js"

src="../engine/engine.js"

回答by vdbuilder

That would be:

那将是:

<script type ="text/javascript" src ="../engine/engine.js"/>