在 HTML 中包含外部 jQuery 文件

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

Including an external jQuery file in HTML

jqueryhtml

提问by aborted

So guys, I guess my question is quite understandable: I want to include an external jQuery file into my HTML. I tried many many ways, which I thought (and still think) were correct, but it doesn't seem to be so.

所以伙计们,我想我的问题很容易理解:我想在我的 HTML 中包含一个外部 jQuery 文件。我尝试了很多很多方法,我认为(并且仍然认为)是正确的,但似乎并非如此。

This is my code:

这是我的代码:

    <script type="text/javascript" src="js/jquery-1.5.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.9.js"></script>
    <script type="text/javascript" src="js/pinkmoon.js"></script>

Where pinkmoon.js is the file that holds my jQuery.

其中 pinkmoon.js 是保存我的 jQuery 的文件。

回答by The Muffin Man

If you navigated to a page that physically resides in a folder structure like so:

如果您导航到物理驻留在文件夹结构中的页面,如下所示:

http://mywebsite.com/folder1/subfolder1/myPage.html

http://mywebsite.com/folder1/subfolder1/myPage.html

Then with your current code the source is going to look like this:

然后使用您当前的代码,源代码将如下所示:

src="http://mywebsite.com/folder1/subfolder1/js/pinkmoon.js"

src="http://mywebsite.com/folder1/subfolder1/js/pinkmoon.js"

Which is not what you want. What you do want is this:

这不是你想要的。你想要的是这样的:

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

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

Which would infer that the physical location of pinkmoon.jsresides at:

这将推断出的物理位置pinkmoon.js位于:

http://mywebsite.com/js/pinkmoon.js

http://mywebsite.com/js/pinkmoon.js

回答by Haza

This should work. Be sure to have the file you want to include in a directory called "js" at the same level of you html file.

这应该有效。确保将要包含的文件包含在与 html 文件处于同一级别的名为“js”的目录中。

For exemple :

举个例子 :

mysite/index.html
mysite/js/pinkmoon.js