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
- $ (Function (){
- $. Fn. Plug-in name = function (options ){
- Var defaults = {
- Event: "click", // trigger the RESPONSE Event
- Msg: "Holle word! "// Display content
- };
- Var options = $. extend (defaults, options );
- Var $ this = $ (this); // certainly responds to the event object
- // Function Code
- // Bind events
- $ This. live (options. Event, function (e ){
- Alert (options. msg );
- });
- }
- });
Step 2: Call the latest plug-in UI front-end framework! Trial with prize !. Code
- $ (Function (){
- // Bind element event
- $ ("# Test"). Plug-In Name ({
- Event: "click", // trigger the RESPONSE Event
- Msg: "The plug-in is so simple! "// Display content
- });
- });
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