JQuery指南/基礎

 

 這是一個基礎指南,旨在幫助你開始使用jquery。jquery給予你常見問題的解決方法。如果你尚未建立你的測試頁面,我建議你創建一個含有下列內容的HTML頁:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
<html>
<head>
<script type="text/javascript"
src="link/to/jquery.js"></script>
<script type="text/javascript">
   // Your code goes here
</script>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
</body>
</html>


      修改script標籤的src屬性指向到你的jquery.js。例如,如果你的jQuery.js與你的HTML文件在同一目錄,你可以這樣:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
<script type="text/javascript" src="jquery.js"></script>
文檔載入時運行代碼
     首先, 大多數JavaScript程序員會用類似代碼:  
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
window.onload = function(){ ... } .

     訪問HTML文檔的元素,必須先載入文檔對象模型(DOM)。當window.onload函數執行的時候,說明所有東西已經載入,包括圖像和橫幅等等。要知道較大的圖片下載速度會比較慢,因此用戶必須等待大圖片下載完畢才能看到window.onload()執行的代碼效果,這樣就花費了很長的等待時間,這不是我們想要的。

        對於此,jquery提供了一個"ready"事件,你可以使用以下的代碼片段:  
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
$(document).ready(function(){
// 你的代碼
});

       $(document)意思是說,獲取文檔對象(類似的於window.document),$(document).ready意思就是說,獲取文檔對象就緒的時候。
       上面這段代碼的意思是檢查文檔對象直到它能夠允許被操作(譯者注:這樣做比window.onload()函數要快的多,因爲只要文檔對象載入完成就能夠執行代碼了,而不需要等待頁面中的圖片下載是否已經完成)---這是我們想要的。因此將上面的代碼片段粘貼到你測試頁面的腳本區吧!

鼠標點擊時的觸發
      
首先,我們嘗試鼠標點擊超鏈接時觸發某些行爲。在ready函數里加入以下代碼:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
$("a").click(function(){
alert("謝謝你的來臨!");
});

       保存HTML文件,然後刷新一下頁面。點擊某個超鏈接,頁面將彈出警告對話框。

增加 CSS Class

       另外一個事情就是,一個共同的任務:增加或移除元素的css class,例如:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
$("a").addClass("test");
$("a").removeClass("test");

    如果你已經在頁面頭部加入了:  
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
<style>a{text-weight:bolder}</style>
    那麼當你調用了addClass函數後,所有超鏈接的字體將變成粗體。

特效
   
Effects Module(效果模塊)提供了一系列好用的特效。

    加上下面代碼:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
$("a").click(function(){
    $(this).hide("slow");
    return false;
});

    現在,只要你點擊超鏈接,超鏈接就會慢慢的消失。“return false"表示保留默認行爲,因此頁面不會跳轉。

回調

   
所謂回調就是父函數執行完成後,自身能夠作爲返回值傳遞到另一個函數的函數。回調功能的特別之處在於,出現在“父函數"後面的函數可以在回調函數執行前執行。
    另外一個重點是要知道如何正確運用回調,我就常常忘記了正確語法。

    一個不帶參數的回調應該這樣寫:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
$.get('myhtmlpage.html', myCallBack);
    注意第二個參數是一個簡單的函數名(它不是字符,也沒有帶括號)

    那麼帶參數的回調該怎麼寫呢?
   
錯誤的寫法,下面這樣寫是不行的(或者不會執行):
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
$.get('myhtmlpage.html', myCallBack(param1,param2));
    正確的寫法:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代碼
$.get('myhtmlpage.html', function(){
    myCallBack(param1,param2);
});

    這樣就實現了回調一個帶參函數的功能。

後記
   
   到這裏,也許你應該去看看其餘的文檔了。裏面包括更多的指南-它很全面,涵蓋了jquery各個方面。如果大家有問題,請放心的給我發Email。
    當然,你也可以看看利用jQuery做的多種DEMO
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章