簡體   English   中英

如何在JavaScript中嵌入jQuery庫?

[英]How to embed a jquery library in javascript?

我在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頁面上。 那我該怎么辦呢?

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

在手冊中了解jQuery的工作原理 ,以及下載頁面以獲取庫(或查找地址以在Content Delivery Network上直接鏈接)。

您只需要在使用腳本文件中定義的函數之前就將它們包括在內( $只是一個函數),例如:

<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的插件之前包含jQuery,否則首先會遇到一些錯誤。

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

將其放在<head></head>中的jQuery代碼上方

<script src="/js/jquery.js" type="text/javascript"></script>
<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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM