jQuery的簡(jiǎn)單使用
網(wǎng)頁(yè)中添加 jQuery
可以通過多種方法在網(wǎng)頁(yè)中添加 jQuery。 您可以使用以下方法:
從 jquery.com 下載 jQuery 庫(kù)
從 CDN 中載入 jQuery, 如從 Google 中加載 jQuery
下載 jQuery
有兩個(gè)版本的 jQuery 可供下載:
Production version - 用于實(shí)際的網(wǎng)站中,已被精簡(jiǎn)和壓縮。
Development version - 用于測(cè)試和開發(fā)(未壓縮,是可讀的代碼)
以上兩個(gè)版本都可以從 jquery.com 中下載。
jQuery 庫(kù)是一個(gè) JavaScript 文件,您可以使用 HTML 的 <script> 標(biāo)簽引用它:
<head>
<script src="jquery-1.10.2.min.js"></script>
</head>
注: 將下載的文件放在網(wǎng)頁(yè)的同一目錄下,就可以使用jQuery。
替代方案
如果您不希望下載并存放 jQuery,那么也可以通過 CDN(內(nèi)容分發(fā)網(wǎng)絡(luò)) 引用它。
百度、又拍云、新浪、谷歌和微軟的服務(wù)器都存有 jQuery 。
如果你的站點(diǎn)用戶是國(guó)內(nèi)的,建議使用百度、又拍云、新浪等國(guó)內(nèi)CDN地址,如果你站點(diǎn)用戶是國(guó)外的可以使用谷歌和微軟。
注:本站實(shí)例均采用百度 jQuery CDN庫(kù)。
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
</head>
實(shí)例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); $("#show").click(function(){ $("p").show(); }); }); </script> </head> <body> <p>歡迎大家來到php.cn</p> <button id="hide">隱藏</button> <button id="show">顯示</button> </body> </html>