如何在javascript中嵌入jquery库?

时间:2010-11-26 09:45:43

标签: javascript jquery

我在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> 

但是jQuery没有运行,我假设因为我没有正确地将jQuery加载到html页面上。那我该怎么做呢?

6 个答案:

答案 0 :(得分:4)

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

请参阅手册中的how jQuery works了解基础知识,download page获取图书馆(或查找内容分发网络上直接链接的地址)。

答案 1 :(得分:1)

您只需要在使用其中定义的函数之前包含脚本文件($只是一个函数),例如:

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

请务必在依赖它的插件之前加入jQuery ,否则您首先会遇到一些错误。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

<script type="text/javascript" src="PATH TO YOUR JS FILE"></script>

将这一点放在<head></head>

中的jQuery代码上方

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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