无参jquery插件的模板:
for example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>不带参数的jquery插件开发</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> (function($){ $.fn.extend({ myPlugName:function(){ $(this).click(function(){ alert($(this).val()); }); } }); })(jQuery); </script> </head> <body> <input type="button" value="点击我" id="btn" />带参数的jquery插件
</body> <script type="text/javascript"> $("#btn").myPlugName(); </script> </html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> (function($){ $.fn.hilight=function(options){ var defaults={ foreground:'red', background:'yellow' }; var opts = $.extend(defaults,options); $(this).css("background-color",opts.background); $(this).css("color",opts.foreground); }; })(jQuery); </script> </head> <body> <div id="myDiv">This is a Params JQuery!</div> </body> <script type="text/javascript"> $("#myDiv").hilight({foreground:'blue'}); </script> </html>
jquery 其中一个标准模板为:
(function($){ $.fn.hilight=function(options){ var defaults={ foreground:'red', background:'yellow' }; var opts = $.extend(defaults,options); //dosomethings }; })(jQuery);