A Brief Introduction to jQuery custom plug-ins and a brief introduction to jquery
JQuery custom plug-in:
Although jQuery provides a variety of convenient functions, it may not be able to meet your needs. In actual applications, you may need to define some more traversal functions by yourself, this is what we call custom plug-ins. The following describes how to implement custom plug-ins.
I. Basic principles for creating plug-ins:
1. Naming principles:
To generate a separate file for the plug-in, we recommend that you use the following naming method:
Jquery. Plug-in name. js
In this way, we can clearly inform other programmers that this is a custom plug-in, for example:
jquery.color.js
2. The instance method or static method:
To attach an instance method to a jQuery. fn object.
If a static method is to be appended to a jQuery object.
For more information about jQuery. fn, see what is the role of jQuery. fn.
For details about the jQuery. extend () function, see jQuery. extend () method.
3. this points:
Within a UDF, this Is The jQuery object obtained by the selector. Therefore, you can use this. each () function to traverse every element in the jQuery object set.
For details about the each () function, refer to the each () method section of jQuery.
For more information about custom plug-in instances, see the instance code section.
4. Usage of semicolons:
A semicolon is used to separate js statements. To prevent errors during code compression, you 'd better use a semicolon or even a semicolon at the beginning of the plug-in.
5. Because chained call is a good feature of jQuery, it is recommended that the custom plug-in return an object to achieve the effect of chained call.
Ii. Simple code example:
1. Plug-in code:
;(function($){ $.fn.extend({ "color":function(value){ return this.css("color",value); } });})(jQuery);
In the above code, you can notice that there is a semicolon at the beginning and end of the code, and declare an anonymous function to pass jQuery objects as parameters.
Use the $. fn. extend () function to extend the Instance Object method. For more information about this method, see the role of jquery. fn. extend () and jquery. extend () functions.
2. Use of plug-ins:
<Script type = "text/javascript"> $ (document ). ready (function () {$ ("# thediv "). color ("red") ;}) </script>
The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 13052.
For more information, see: http://www.softwhy.com/jquery/