自己动手做jQuery插件

前言:这种东西随意可以在网上收到,这里我还是只是记下自己的见解和领。

第一种方式

1 (function ($) {
2     $.extend({
3         sayHello: function (name) {
4              alert(name + ": hello");
5         }
6     });
7 
8   
9 })(jQuery);
$.sayHello("meng");

这种属于静态方法,直接调用即可。

第二种方式:

1 (function ($) {
2 
3     $.fn.redBg = function () {
4         $(this).css("background", "red");
5     };
6 
7    
8 })(jQuery);
1 $("#box").redBg();

说明:这两种方式类似我的这篇博文中的两种

http://www.cnblogs.com/chenluomenggongzi/p/5827268.html

正如网上都会有的一句话

第三种,两种方式的结合

 1 (function ($) {
 2 
 3     $.fn.changeFontColorAndSize = function (options) {
 4         var defaults = {
 5             "color": "white",
 6             "fontSize": "24px"
 7         };
 8         var settings = $.extend({}, defaults, options);
 9         return this.css({
10             "color": settings.color,
11             "fontSize": settings.fontSize
12         })
13     }
14 })(jQuery);
$("#box").changeFontColorAndSize();
原文地址:https://www.cnblogs.com/chenluomenggongzi/p/5928871.html