<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head> <title></title> <Scripttype= "Text/javascript"src= "Scripts/jquery-1.4.1.js"></Script> <Scripttype= "Text/javascript"> $(function () { (function ($) { varMethods={show:function() {alert ('This is show'); }} $.fn.test= function(opt) {varSettings={color:"Red", FontSize:'36px', refresh:function() {alert ('This is settings refresh'); } }; if (typeofopt== "string") {alert (Array.prototype.slice.call (arguments,1)); returnmethods[opt].apply ( This, Array.prototype.slice.call (arguments,1)); //Array.prototype.slice.call (arguments, 1) is the get parameter 1 for the second argument } Else { varoptnew=$.extend (settings, opt); //There is no need to have this package in the $ number as $ (this), because this is already a jquery object. //$ (this) equivalent to $ ($ (' #element '))alert (optnew.fontsize); $( This). css ({color:optnew.color,"font-size": Optnew.fontsize}); //Settings.refresh (); alert (Optnew.refresh); } }; }) (JQuery); //$ (' div '). Test ({color: ' Blue ', fontSize: "100px", Refresh:function () {alert (' This is opt refresh ');}); $('Div'). Test ("Show","This is 2 Canshu"); //the method that invokes the plug-in first parameter is the method name, the second parameter is an additional parameter that can be passed to show }) </Script></Head><Body><Div>Test</Div></Body></HTML>
Writing jquery Plugins