如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:53:16  来源:igfitidea点击:

How to embed a jquery library in javascript?

javascriptjquery

提问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).

jQuery的运行手册中的基本知识,以及下载页面来获取库(或找出直接链接地址上的内容分发网络)。

回答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>