This example describes the way to build your own jquery plug-in. Share to everyone for your reference, specific as follows:
Write JS plug-in is not troublesome, JS Plug-ins have two types, type Plug-ins and object plug-ins. First, for example:
This is a type plug-in, in other words, the plug-in does not need to specify an object when invoked. So:
This is an object plug-in, which means that you need to specify the object when you use the plugin.
When writing jquery plug-ins, there are fixed templates:
Jquery.plugin1 = function (text) {
alert (text);
};/ /type-level framework, similar to $.post ();
$.fn.plugin2 = function (options) {
var defaults = {
text: ' Hello, world! '
};
Merge default value
var opts = $.extend (defaults, options);
Your code is written here
alert (opts.text);
};/ /object-level framework, similar to $ (this). Click ();
The above is plugin1 is a type-level plug-in used when direct call on it, Plugin2 is an object-level framework, the use of time also need objects.
JQuery in the jquery.plugin1 when writing the type-level framework. It cannot be omitted, but also when writing the object-level framework, $.fn. is also not to be omitted.
Here's what I'm going to say. var opts = $.extend (defaults, Options) in object-level framework; This sentence must have, the meaning of this sentence is to merge parameters. Use the parameters in the default value when the user does not set the parameter, otherwise the parameter set by the user is used. Let's look at the calling method:
<script type= "Text/javascript" src= "Http://code.jquery.com/jquery-latest.min.js" ></script>
< Script type= "Text/javascript" src= "/myplugin.js" ></script>
<script type= "Text/javascript" >
$.plugin1 (' Hello ');
$ (document). Ready (function () {
$ (' #tmp '). Click (function () {
$ (this). Plugin2 ({
text: ' http:// Www.jb51.net '
}
); </script>
First of all, when using your own jquery plugin, you need to include it, and then include jquery, you know. The back is called method Oh!
More interested readers of jquery-related content can view the site topics: "jquery window Operation tips Summary", "jquery drag and drop effects and tips summary", "jquery common Plug-ins and Usage summary", "jquery Ajax Usage Summary", "jquery Table" ( Table) Summary of operations tips, jquery Extended techniques Summary, jquery Common Classic effects summary, jquery animation and special effects usage summary and jquery selector Usage Summary
I hope this article will help you with the jquery program design.