如何在 javascript 中嵌入 jquery 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4284137/
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
How to embed a jquery library in javascript?
提问by Hick
I have a jQuery library code in jquery.xyz.js . I have an html file which uses the function defined in jquery.xyz.js in this manner .
我在 jquery.xyz.js 中有一个 jQuery 库代码。我有一个 html 文件,它以这种方式使用 jquery.xyz.js 中定义的函数。
<html>
<body>
<script type="text/javascript">
document.write("This is my first JavaScript!");
$(function(){ $("ul#xy01").xyz(); });
</script>
</body>
</html>
But the jQuery is not running, which I am assuming because I haven't loaded the jQuery properly onto the html page. So how do I do it?
但是 jQuery 没有运行,我假设这是因为我没有将 jQuery 正确加载到 html 页面上。那么我该怎么做呢?
回答by Pekka
<script type="text/javascript" src="jquery.js"></script>
See how jQuery worksin the manual for the basics, and the download pageto fetch the library (or to find out addresses for direct linking on a Content Delivery Network).
回答by Nick Craver
You just need to include the script files before using functions defined in them ($is just a function), for example:
您只需要在使用其中定义的函数之前包含脚本文件($只是一个函数),例如:
<html>
<body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.xyz.js"></script>
<script type="text/javascript">
$(function(){ $("ul#xy01").xyz(); });
</script>
</body>
</html>
Be sure to include jQuery beforeplugins that rely on it, or you'll get some errors first thing.
确保在依赖它的插件之前包含 jQuery ,否则你会首先得到一些错误。
回答by samrith.v
<script>
var script = document.createElement('script');
script.onload = function () {
//do stuff with the script
};
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/xx.xx/jquery.min.js';
document.head.appendChild(script);
</script>
回答by benhowdle89
<script type="text/javascript" src="PATH TO YOUR JS FILE"></script>
Stick this above your jQuery code in the <head></head>
将此粘贴在您的 jQuery 代码上方 <head></head>
回答by demas
<script src="/js/jquery.js" type="text/javascript"></script>
回答by slobodan
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">
</script>
</head>
<body>
<script type="text/javascript">
document.write("This is my first JavaScript!");
$(function(){ $("ul#xy01").xyz(); });
</script>
</body>
</html>

