jQuery plugin 简单探索

发布于 2015-09-23 / 前端 / 0条评论 / 2,306浏览

jQuery 一个流行的JS库。从工作到现在,接触最多的一个js库,有时候难免会用其他的第三方的插件,我自己也略懂一些,就此花了点时间写了点简单的实现jQuery插件的demo。
内容不多,可以直接复制代码保存成html文件在本地打开。

<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript" > (function($){ // 全局 $.denghb = function(){ console.log('denghb'); } // 方法 $.fn.denghb = function(param){ $(this).append('fn denghb '+param); } // 扩展 $.extend({ ext_denghb:function(param){ console.log('extend denghb '+param); } }); $.fn.extend({ ext_fn_denghb:function(){ $(this).append('<br/>ext fn denghb'); } }); // $.fn.style = function(options){ var defaults = { background:'#eee', color:'red' } options = $.extend(defaults,options); $(this).css(options); } // more ... //console.log($); })(jQuery); $(function(){ $.denghb(); $('body').denghb('123'); $.ext_denghb(321); $('body').ext_fn_denghb(); $('#adv').style({background:'#fff'}); }); </script> </head> <body> <div id="adv" style="width:100px;height:100px;"> hello jquery plugin </div> </body> </html>

动手后才发现世界上没有一件简单的事情

评论
站长统计