码迷,mamicode.com
首页 > 其他好文 > 详细

自己动手做jQuer插件

时间:2016-10-03 12:30:42      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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

第一种方式

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();

 

自己动手做jQuer插件

标签:

原文地址:http://www.cnblogs.com/chenluomenggongzi/p/5928871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!