jq插件封装学习笔记
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> (function($){ $.fn.showName=function(obj){ //命名空间$.fn。使得showName每一个对象都有showName方法 var df={ //定义了一个保存默认值对象 name:"佚名", age:18, job:"*" } df=$.extend(df,obj); //合并showName传人的参数 //console.log($); //这里的$相当this console.log("my name is "+df.name+"年龄是 "+df.age+"job "+df.job); }; })(jQuery); //兼容$.fn与jQuery.fn </script> </head> <body> <a id="article-content"></a> <a></a> <a></a> </body> </html> <script type="text/javascript"> $("a").showName({name:"wang",age:24,job:"java"}); $("*").showName({name:"li",age:32,job:"c"}); $("body").showName({name:"zhang",age:45,job:"php"}); $("*").showName(); /* my name is wang年龄是 24job java my name is li年龄是 32job c my name is zhang年龄是 45job php my name is 佚名年龄是 18job * */ </script>
新手学习笔记,有问题之处望前辈们批评指正!谢谢!