Create your own jquery Plugin starter Tutorial _jquery

Source: Internet
Author: User
Tags extend

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:

$.post (URL);

This is a type plug-in, in other words, the plug-in does not need to specify an object when invoked. So:

$ (' P '). click ();

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.

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.