1(function ($) {2 $.fn.extend ({3"HighLight":function(options) {4 //checks whether the user passed in the parameter is legitimate5 if(!isValid (options))6 return This;7 varopts = $.extend ({}, defaluts, options);//overriding plug-in default parameters with Jquery.extend8 return This. each (function() {//This is the jquery object. Return here to support chained calls9 //iterate through all the DOM to be highlighted when calling the HighLight () plugin is a collection. Ten var$ This= $( This);//gets the current Dom's jquery object, where this is the DOM of the current loop One //set the DOM style according to the parameters A$ This. css ({ - BackgroundColor:opts.background, - Color:opts.foreground the }); - //formatting highlighted text - varMarkup = $ This. html (); -Markup =$.fn.highlight.format (markup); +$ This. HTML (markup); - }); + A } at }); - - - //Default Parameters - varDefaluts = { -Foreground: ' Red ', inBackground: ' Yellow ' - }; to //public formatting methods. The default is bold, and the user can override the method to achieve different formatting effects. +$.fn.highlight.format =function(str) { - return"<strong>" + str + "</strong>"; the } * //private methods to detect if the parameters are legitimate $ functionisValid (options) {Panax Notoginseng return!options | | (Options &&typeofoptions = = = "Object")?true:false; - } the}) (Window.jquery);
jquery Plugin Development Demo