Javascript 如何在另一个javascript文件中包含jquery

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

how to include jquery in another javascript file

javascriptjqueryinclude

提问by cagin

I have a javascript file and I want to include jquery in this js file. Do you have a suggestion?

我有一个 javascript 文件,我想在这个 js 文件中包含 jquery。你有什么建议吗?

回答by Michael Aaron Safyan

Simply include the JavaScript for jQuery before you include your own JavaScript:

在包含您自己的 JavaScript 之前,只需包含 jQuery 的 JavaScript:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="path/to/your/script.js"></script>

Though others have suggested simply copying and pasting jQuery into your own JavaScript file, it's really better to include jQuery separately from a canonical source as that will allow you to take advantage of the caching that your browser and intermediate servers have done for that file.

尽管其他人建议简单地将 jQuery 复制并粘贴到您自己的 JavaScript 文件中,但将 jQuery 与规范源分开包含确实更好,因为这将允许您利用浏览器和中间服务器为该文件所做的缓存。

回答by neaumusic

var jq = document.createElement("script");

jq.addEventListener("load", proceed); // pass my hoisted function
jq.src = "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js";
document.querySelector("head").appendChild(jq);

function proceed () {
    // jQuery load complete, do your magic


}

回答by Quentin

Just merge the files.

只需合并文件。

cat jquery.js >> a_javascript_file.js

Or, since you probably want jQuery first:

或者,因为您可能首先需要 jQuery:

cat a_javascript_file.js jquery.js > tmp.js;
mv tmp.js a_javascript_file.js

回答by kasper Taeymans

If you want to include a js file into a js file... javascript does not have something like an include function but you can load the file with an ajax request or add a script tag to the html with js. Check out the following link, a really interesting read!

如果你想将一个 js 文件包含到一个 js 文件中...... javascript 没有类似包含函数的东西,但你可以使用 ajax 请求加载文件或使用 js 向 html 添加脚本标记。查看以下链接,非常有趣的阅读!

How do I include a JavaScript file in another JavaScript file?

如何在另一个 JavaScript 文件中包含一个 JavaScript 文件?

回答by DK Chauhan

I suggest you to use a line of code inside your java script library.

我建议您在 Java 脚本库中使用一行代码。

document.write("Script/jquery.min.js'></script>");

with the help of this, you do not need to add references of two files on your page.

借助此功能,您无需在页面上添加两个文件的引用。