如何寫jquery外掛程式

來源:互聯網
上載者:User

標籤:jquery   jquery外掛程式   

常用的JQuery外掛程式有如下幾種寫法:


1. 對JQuery自身的擴充外掛程式


這種外掛程式是對JQuery自身的方法庫進行擴充的。在使用的時候通過$.MethodName()的方式直接使用。


$.extend({    handleTableUI : function(table){        var thisTable = $("#" + table);                $(thisTable).find("tr").bind("mouseover", function () {            $(this).css({ color: "#ff0011", background: "blue" });        });        $(thisTable).find("tr").bind("mouseout", function () {            $(this).css({ color: "#000000", background: "white" });        });    }});


說明


當要對JQuery自身進行擴充的時候,需要採用$.extend();的形式進行開發,JQuery的extend()方法為我們提供了擴充 JQuery自身的方式,在extend()方法中,我們採用{...}的形式編寫具體的方法體。其中,最重要的是要定義我們自己的擴充方法,如樣本中的 handleTableUI。定義的方式是:方法名 : function(參數){ 方法體 }。通過此種方式我們就可以定義JQuery自己的擴充方法,而且這個方法可以在web頁面通過智能提示顯示出來。



頁面中調用的代碼如下:


<script type="text/javascript">    $(document).ready(function () {        $.handleTableUI("newTable");    });</script>




2、 對JQuery對象的外掛程式開發


(function ($) {    $.fn.setTableUI = function(options){        var defaults = {            evenRowClass:"evenRow",            oddRowClass:"oddRow",            activeRowClass:"activeRow"        }        var options = $.extend(defaults, options);        this.each(function(){            var thisTable=$(this);            $(thisTable).find("tr").bind("mouseover", function () {                $(this).css({ color: "#ff0011", background: "blue" });            });            $(thisTable).find("tr").bind("mouseout", function () {                $(this).css({ color: "#000000", background: "white" });            });        });    };})(jQuery);



具體調用代碼如下:


<script type="text/javascript">    $(document).ready(function () {        $("#newTable").setTableUI();    });</script>



參考資料:  jquery外掛程式寫法   http://www.studyofnet.com/news/500.html


本文出自 “學習也休閑” 部落格,請務必保留此出處http://studyofnet.blog.51cto.com/8142094/1439453

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.