1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // this code creates the Closure structure (function(jQuery) { // this function is "private" function formatCodes(locale) { // plug-in specific code here }; // don't forget the semi-colon // this method is "public" because it's attached to the jQuery object jQuery.fn.format = function(options) { var options = jQuery.extend( { format: "#,###.00", locale: "us" },options); return this.each(function(){ var text = new String(jQuery(this).text()); if (jQuery(this).is(":input")) text = new String(jQuery(this).val()); // you can call the private function like any other function var formatData = formatCodes(options.locale.toLowerCase()); // plug-in-specific code here }); }; // don't forget the semi-colon to close the method // this code ends the Closure structure })(jQuery); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | jQuery.fn.format = function(options) { // Change how you load your options in to take advantage of your overridable defaults // You change how your extend() function works, because the defaults // are globally defined, rather than within the method. If you didn't use the // {} as the first argument, you'd copy the options passed in over the defaults, which is // undesirable. This {} creates a new temporary object to store the options // You can simply call the defaults as an object within your plug-in var options = jQuery.extend({},jQuery.fn.format.defaults, options); return this.each(function(){ // rest of the plug-in code here // define the defaults here as an object in the plug-in jQuery.fn.format.defaults = { format: "#,###.00", locale: "us" }; // don't forget the semi-colon |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | (function(jQuery) { function FormatData(valid, dec, group, neg) { this.valid = valid; this.dec = dec; this.group = group; this.neg = neg; }; function formatCodes(locale) { // format logic goes here return new FormatData(valid, dec, group, neg); }; jQuery.fn.parse = function(options) { var options = jQuery.extend({},jQuery.fn.parse.defaults, options); var formatData = formatCodes(options.locale.toLowerCase()); var valid = formatData.valid; var dec = formatData.dec; var group = formatData.group; var neg = formatData.neg; var array = []; this.each(function(){ var text = new String(jQuery(this).text()); if (jQuery(this).is(":input")) text = new String(jQuery(this).val()); // now we need to convert it into a number var number = new Number(text.replace(group,'').replace(dec,".").replace(neg,"-")); array.push(number); }); return array; }; jQuery.fn.format = function(options) { var options = jQuery.extend({},jQuery.fn.format.defaults, options); var formatData = formatCodes(options.locale.toLowerCase()); var valid = formatData.valid; var dec = formatData.dec; var group = formatData.group; var neg = formatData.neg; return this.each(function(){ var text = new String(jQuery(this).text()); if (jQuery(this).is(":input")) text = new String(jQuery(this).val()); // formatting logic goes here if (jQuery(this).is(":input")) jQuery(this).val(returnString); else jQuery(this).text(returnString); }); }; jQuery.fn.parse.defaults = { locale: "us" }; jQuery.fn.format.defaults = { format: "#,###.00", locale: "us" }; })(jQuery); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |