Recently many netizens say jquery plugin is what ah? How do you write that? I can't write? A lot of questions do not know how to answer them at the moment, the personal think is the netizen to complicate the problem. In fact, some of the commonly used, practical, universal functions encapsulated and, in short, is to put these code in a method, you can achieve the effect of repeated use, so that you do not need to use this function every time to write again.
Now jquery is added to the concept of plug-ins, as long as it is in accordance with its specific format as usual writing function to write it, not though it is too complex. Believe it or not, I believe it anyway. Next with a simple code to explain, if the netizens have read the plugin will not write, I can only no language
The first step: Define the plug-in. Code
- $ (function () {
- $.fn. Plug-in name = function (options) {
- var defaults = {
- Event: "click",//Trigger response event
- Msg: "Holle word!" //display content
- };
- var options = $.extend (defaults,options);
- var $this = $ (this); Of course the response event object
- Function Code Section
- Binding events
- $this. Live (options. Event,function (e) {
- alert (options.msg);
- });
- }
- });
Step Two: Call the plugin. Code
- $ (function () {
- Binding element Events
- $ ("#test"). Plug-in name ({
- Event: "click",//Trigger response event
- Msg: "The plugin was so simple!" //display content
- });
- });
After reading, I believe that netizens should understand it! The jquery plugin was so simple
10 minutes to learn to write jquery plugins