2009-09-29 76 views
1

我正在嘗試爲jQuery編寫幾個插件,所以作爲一個起點,我去了文檔(here)中顯示的基本示例。這是代碼我有,在一個名爲jquery.test.js js文件:jQuery插件創作:無法獲得基本示例工作?

(function($) { 

    $.fn.myPlugin = function(settings) { 
    var config = {'foo': 'bar'}; 

    if (settings) $.extend(config, settings); 

    this.each(function() { 
     // element-specific code here 
     alert('found P tag'); 
    }); 

    return this; 

    }); 

})(jQuery); 

而這裏的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html lang="en"> 
    <head> 
     <title>Plugin Test</title> 
     <script type="text/javascript" src="/jquery-1.3.2.min.js"></script> 
     <script type="text/javascript" src="/jquery.test.js"></script> 
     <script type="text/javascript"> 
     $(document).ready(function(){ 

      $('p').myPlugin(); 
     }); 
     </script> 

    </head> 
    <body> 
     <p>asdgasdgasg</p> 
    </body> 
</html> 

我預計這將彈出一個警告,每款標籤在頁面中。但是,當我加載頁面時,沒有任何反應。螢火蟲給我兩個錯誤:

missing ; before statement 
http://playground.darthvader.com/jquery.test.js 
Line 16 

$("p").myPlugin is not a function 
http://playground.darthvader.com/ol.html 
Line 11 

我很困惑。任何人都可以看到我在這裏做錯了嗎?

編輯:事實證明,有一個錯誤的jQuery文檔(以及抓到Ghommey),我從中複製和粘貼代碼。我在文檔站點聯繫了管理員,現在它已經修復。

回答

4
return this; 

    }); 

似乎是錯的 - 只嘗試:

return this; 
    }; 
+0

是的,你是對的 - jQuery的文檔有一個錯字!謝謝,我瘋了...... – 2009-09-29 15:25:51