Let netizens learn to write Jquery plug-ins and jquery plug-ins in 10 minutes

Source: Internet
Author: User

Let netizens learn to write Jquery plug-ins and jquery plug-ins in 10 minutes
Recently, many netizens have said what the jquery plug-in is? How did you write it? I can't write? I don't know how to answer a lot of questions at the moment. I personally think that netizens have complicated the problem. In fact, it is to encapsulate some common, practical, and general functions. Simply put, it is to put these codes in a method to achieve repeated use, in this way, you do not need to re-write each time you use this function. The latest UI front-end framework! Trial with prize! Now Jquery has added the plug-in concept, as long as it is written in the same way as function writing in its specific format. It is not complicated. Believe it or not, but I believe it. Next, I will explain it with simple code. If the netizens do not write any plug-ins, I will be speechless.

Step 1: Define the plug-in. Code

  1. $ (Function (){
  2. $. Fn. Plug-in name = function (options ){
  3. Var defaults = {
  4. Event: "click", // trigger the RESPONSE Event
  5. Msg: "Holle word! "// Display content
  6. };
  7. Var options = $. extend (defaults, options );
  8. Var $ this = $ (this); // certainly responds to the event object
  9. // Function Code
  10. // Bind events
  11. $ This. live (options. Event, function (e ){
  12. Alert (options. msg );
  13. });
  14. }
  15. });
 

Step 2: Call the latest plug-in UI front-end framework! Trial with prize !. Code
  1. $ (Function (){
  2. // Bind element event
  3. $ ("# Test"). Plug-In Name ({
  4. Event: "click", // trigger the RESPONSE Event
  5. Msg: "The plug-in is so simple! "// Display content
  6. });
  7. });
 

After reading this, I believe that netizens should understand it! Jquery plug-in is so simple
How to write the jquery plugin

How does jQuery write plug-ins-

There are two types of jQuery plug-in development:

One is class-level plug-in development, that is, adding a new global function to jQuery is equivalent to adding a method to the jQuery class itself. JQuery's global functions belong to the jQuery namespace, and the other is object-level plug-in development, that is, adding methods to jQuery objects. The following describes the development of the two functions in detail.

1. Class-level plug-in development

The most direct understanding of class-level plug-in development is to add class methods to the jQuery class, which can be understood as adding static methods. A typical example is the $. AJAX () function, which is defined in the namespace of jQuery. The following extensions can be used for class-level plug-in development:

1.1
Add a new global function

To add a global function, we only need to define it as follows:

JQuery. foo =
Function (){
Alert ('this is a test. This is only
Test .');
};

1.2
Add multiple global functions

Add multiple global functions, which can be defined as follows:
Java code collection code

JQuery. foo = function (){
Alert ('this is a test. This is
Only a test .');
};
JQuery. bar =
Function (param ){
Alert ('this function takes
Parameter, which is "'+ param + '".');
};
The call time is the same as that of a function: jQuery. foo (); jQuery. bar (); or $. foo (); $. bar ('bar ');

1.3
Use jQuery. extend (object );

JQuery. extend ({
Foo:
Function (){
Alert ('this is a test. This is
Only a test .');

},
Bar: function (param)
{
Alert ('this function takes
Parameter, which is "'+ param + '".');

}
});

1.4
Use namespace

Although a large number of javaScript function names and variable names are not allowed in the jQuery namespace. But it is still inevitable that some functions or variables will conflict with other jQuery plug-ins, so we are used to encapsulating some methods into another custom namespace.

JQuery. myPlugin =
{
Foo: function ()
{

Alert ('this is a test. This is only
Test .');

},

Bar: function (param)
{

Alert ('this function takes a parameter, which is "'+ param +
'".');

}
};
The namespace function is still a global function.
 
Question about jQuery plug-in writing

$. Fn. test = function (json, fun ){
Var obj = "I am the this object in the function ";
Var param1 = "I'm a test string"
If (typeof (fun) = 'function '){
Fun. call (obj, param1 );
}
}

$ ("# Div1"). test ({"a": "B"}, function (param1 ){
Alert (this );
Alert (param1)
});
<Div id = "div1"> I am a layer </div>
Test function parameter 2: function
Use the call method to activate a function,
The first parameter of the call method is the this object in your method.
The second parameter is the parameter in your method.
For more information, see the help documentation.
You will understand the two examples of the upper and lower sides in the test.
If there is something you don't understand: linv2@vip.qq.com

$. Fn. test = function (json, fun ){
// Var obj = "I am the this object in the function ";
Var param1 = "I'm a test string"
If (typeof (fun) = 'function '){
Fun. call (this, param1 );
}
}

$ ("# Div1"). test ({"a": "B"}, function (param1 ){
Alert ($ (this). attr ("id "));
Alert(%(this%.html ());
Alert (param1)
});
<Div id = "div1"> I am a layer </div>
References: Experience

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.