Jquery plug-in development general framework, jquery plug-in framework
Jquery plug-in development framework code:
/** INS: * 1. Plug-In Name: jquery. [plug-in name]. js, such as jquery. plugin. js * 2. Add the object method to jQuery. on fn, add the global method to the jQuery object itself. * 3. this inside the plug-in points to the jQuery object obtained through the selector * 4. You can use this. each traverses all elements * 5. All methods end with a semicolon. Add a semicolon * 6 to the plug-in header. The plug-in should return a jQuery object, to ensure the chained operation of the plug-in * 7. Use the closure form, and use $ as the alias of jQuery to avoid conflicts * // for good compatibility, there is a semicolon before the start; // The jQuery object is passed in to ensure that the jQuery object is used correctly in the anonymous function and prevent the $ conflict when multiple libraries coexist. // it is not required to pass in the window and document, however, in order to access the window and document more quickly, the undefined parameter is not modified because the parameter is not actually passed. (Function ($, win, doc, undefined) {var alertPlugin = function (opts) {// save this variable var _ self = this; // define the default parameter _ self. opts = {content: 'pop-up content'}; // extend the default parameter if (opts & $. isPlainObject (opts) {$. extend (_ self. opts, opts);} // call Function Method _ self. show (_ self. opts) ;}; // function method implementation alertPlugin. prototype = {show: function (opts) {alert (opts. content) ;}} // extension plug-in $. fn. extend ({alertPlugin: function (opts) {// traverse all elements return this. each (function () {new alertPlugin (opts) ;}}) ;}) (jQuery, window, document); // jQuery, window, and document are real parameters.
Plug-in call:
// Call without passing parameters $ ('. A '). alertPlugin (); // call the passed parameter $ ('. A '). alertPlugin ({content: 'New pop-up content '});