Step by step to teach you to write a jquery plugin tutorial (Plugin) _jquery

Source: Internet
Author: User
Tags closure extend

The plugin development of jQuery requires attention,
1. The explicit jquery namespace has only one.
2. Understand the options parameter to control the behavior of the plugin.
3. Provide public access to the default plugin settings.
4. Provide public access to the child functions.
5. Private functions are definitely private access
6. Support Metadata plugin.
I will explain the above conditions in one of the following examples, and after these things we will create a simple plugin that highlights text.

1. The explicit jquery namespace has only one

In our example, we'll name the plugin named Hilight,

That is, our plugin can be used in the following ways:

Why is there only one namespace for the plugin of jquery? It may be a design requirement, or it can be more readable, or for object-oriented design patterns.

2. Understand the options parameters to control plugin behavior.

Let's first clarify the color of foreground and background for our hilight plugin. We should be able to allow these two option to be passed to the plugin main function as the option object. For example:

The plug-in can now set the following properties:

3. Provide public access to the default plugin settings.

We can improve here, so that the above code can be set to expand. This allows users who use this plug-in to overload our option with the least amount of code. That's the benefit of getting started with function objects.

Users can now use one line of code in their script to set the foreground property:

With the above code, we can set the foregrounf color of a DOM control to blue.

4. Provide public access to child functions

This article is similar to the above, it is interesting to let your plugin have extended function. For example, in the plugin of lilight we can define a function as format, which defines the form of hilight text. Our plugin code will display the following:

Here we can easily support another option object to overload the default formatting with a callback function. That would be another nice way to support customization.

5. Private functions are definitely private access

It is certainly a very powerful feature that some of the option of public plugin can be customized. But you need to consider which part should be made public and which part should not be accessed externally, or it will break the result that you have encapsulated.

The Debug method here is not accessible from the outside because he is a private method in the plugin presentation.

6. Support Metadata plugin.

Using metadata plugin needs to see what type of plugin you have, which may make your plug-in more powerful. Personally, I prefer metadata plugin because it can accidentally let my plguin option pass the tag overload.

If the metadata plugin can successfully encapsulate to our plugin, then you can use the Lilight plugin using the tag below.

The final code is as follows:

Copy Code code as follows:

//
Create closure
//
(function ($) {
//
Plugin definition
//
$.fn.hilight = function (options) {
Debug (this);
Build main options before element iteration
var opts = $.extend ({}, $.fn.hilight.defaults, options);
Iterate and reformat each matched element
Return This.each (function () {
$this = $ (this);
Build element Specific Options
var o = $.meta? $.extend ({}, OPTs, $this. Data ()): opts;
Update element styles
$this. CSS ({
Backgroundcolor:o.background,
Color:o.foreground
});
var markup = $this. HTML ();
Call our Format function
Markup = $.fn.hilight.format (markup);
$this. HTML (markup);
});
};
//
Private Function for debugging
//
function Debug ($obj) {
if (window.console && window.console.log)
Window.console.log (' Hilight selection count: ' + $obj. Size ());
};
//
Define and expose our Format function
//
$.fn.hilight.format = function (TXT) {
Return ' <strong> ' + txt + ' </strong> ';
};
//
Plugin defaults
//
$.fn.hilight.defaults = {
Foreground: ' Red ',
Background: ' Yellow '
};
//
End of closure
//
}) (JQuery);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.