標籤: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