jQuery外掛程式開發

來源:互聯網
上載者:User

              1.1、添加靜態方法

  1. jQuery.extend(object);

複製代碼

       為擴充jQuery類本身,為類添加新的方法,可以理解文添加靜態方法。

  1. $.extend({
  2.   addMethod : function(a, b){return a + b;}   // $.addMethod(1, 2); //return 3
  3. });

複製代碼

       1.2、新增成員方法

  1. jQuery.fn.extend(object);

複製代碼

  1. jQuery.fn = jQuery.prototype

複製代碼

       給jQuery對象添加方法,對jQuery.prototype進行擴充,為jQuery類新增成員方法:

  1. $.fn.extend({
  2.     getInputText:function(){
  3.         $(this).click(function(){
  4.             alert($(this).val());
  5.         });
  6.     }
  7. });
  8. $("#username").getInputText();

複製代碼

       2、一個通用的架構:

       以下是一個通用的架構:

  1. (function($){
  2.     $.fn.yourPluginName = function(options){
  3.         //各種屬性和參數
  4.         var options = $.extend(defaults, options);
  5.         this.each(function(){
  6.             //外掛程式的實現代碼
  7.         });
  8.     };
  9. })(jQuery);

複製代碼

       關於

  1. $.extend(defaults, options);

複製代碼

       就是通過合并defaults和options來擴充defaults,實現外掛程式預設參數的功能,詳細解釋可以參考jQuery的官方文檔:
http://api.jquery.com/jQuery.extend/

原文:http://www.itzhai.com/jquery-plug-ins-to-achieve-the-methods-and-principles-of-simple-instructions.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.