<script type= "Text/javascript" > (Function ($) { $.fn.extend ({ myplugname:function () { $ (this . Click (function () { alert ($ (this). Val ());});});}) (jQuery); </script>
<body> <input type= "button" value= "click Me" id= "btn"/>
</body> <script type= "Text/javascript" > $ ("#btn"). Myplugname (); </script>
<script type="Text/javascript">(function ($) {$.fn.hilight=function (options) {vardefaults={foreground:'Red', background:'Yellow' }; varopts =$.extend (defaults,options); $( This). CSS ("Background-color", Opts.background); $( This). CSS ("Color", Opts.foreground); }; }) (JQuery); </script><body> <div id="mydiv">this isA Params jquery!</div></body><script type="Text/javascript"> $("#myDiv"). Hilight ({foreground:'Blue'});</script>
var opts = $.extend (defaults,options); The defaults properties and Options properties are merged and saved to opts
Since the dollar sign ("$") is not only used by the jquery library, other JavaScript libraries may also be used, and if "$" in other libraries has a special meaning, it can cause unnecessary conflicts.
(function ($) { $.fn.mypluginname = function () { //Your plugin logic };}) (JQuery);
1Adding a JQuery object-level plug-in is a way to add a method to the jquery class; (function ($) {$.fn.extend ({"Name of function": Function (custom parameter) {//Write the plugin code here } }); }) (JQuery); or;(function ($) {$.fn. Name of the function=function (custom parameter) {//Write the plugin code here}}) (JQuery); Calling Method: $ ("#id"). function name (parameter);2the. jquery class-level plug-in is equivalent to adding a static method; (function ($) {$.extend ({"Name of function": Function (custom parameter) {//Write the plugin code here } }); }) (JQuery); or;(function ($) {$. function name=function (custom parameter) {//Write the plugin code here}}) (JQuery); Calling method: $. Function name (parameter);
Types of jquery plugins:
1. Object-level plug-in development, that is, to add methods to the JQuery object, encapsulating the object method plug-in, such as: parent (), AppendTo ()
2. One is a class-level plug-in development that adds a new global function to jquery, equivalent to adding a method to the jquery class itself, the global function of jquery is a function belonging to the jquery namespace, a plug-in that encapsulates a global function
3. Selector plug-in
jquery plugin mechanism
jquery has two methods for developing plug-ins, namely:
JQuery.fn.extend (object); Adds a method to a jquery object.
Jquery.extend (object); To extend the jquery class itself. Adding a new method to a class can be understood as adding a static method.
Both of these methods accept a parameter, the type Object,object corresponding to the "name/value pair" represents "function or method body/function Body", respectively.
1. Object-level plug-in development,
The method here provides a parameter value, if the method is called when the value is passed in, then the value is used to set the font color, or to get a match without this font color value
<span style="font-size:12px; >;(function ($) { $.fn.extend ({"color": function (value) { returnthis. css ("color", value); } ); }) (jQuery); </span>
The call can be directly written as : $ ("div"). Color ("red");
In addition, if you want to define a set of plugins, you can use the following notation:
<span style="font-size:12px;">;(function ($) {$.fn.extend ({"Color": function (value) {//Write the plugin code here }, "Border": function (value) {//Write the plugin code here }, "background": function (value) {//Write the plugin code here } }); }) (jQuery);</span>
2. Plug-ins that encapsulate global functions
For example, two new functions are added to remove the left and right spaces.
<span style="font-size:12px;">;(function ($) {$.extend ({ltrim:function (text) {return(text| |""). Replace (/^\s+g,""); }, Rtrim:function (text) {return(text| |""). Replace (/\s+$/g,""); } }); }) (JQuery); or;(function ($) {$.ltrim=function (text) {return(text| |""). Replace (/^\s+g,""); }, $.rtrim=function (text) {return(text| |""). Replace (/\s+$/g,""); }}) (JQuery);</span>
Call Function:
<span style="font-size:12px;">$ ("trimtest"). Val (Jquery.trim ("Test")+"\ n"+Jquery.ltrim ("Test")+"\ n"+Jquery.rtrim ("Test") );</span>
The spaces on the left and right sides of the first string in the text box are deleted.
The second line of the string is deleted only on the left side of the space.
The third row of strings is deleted only on the right side of the space.
jquery Custom Plugins