This article only describes how to compile your own plug-ins. As for the functions to be implemented, they may vary from person to person...
JQuery plug-ins are mainly divided into three types:
1. Plug-ins that encapsulate object Methods
2. Plug-ins that encapsulate global functions
3. Extension selector plug-in
Here we only write the first two types, which are more common ..
Most plug-ins are written in this form:
Copy codeThe Code is as follows:
(Function ($ ){
/* Place the code here */
}) (JQuery );
The advantage is that the function can still use $ as the alias of jQuery, without affecting the usage of $
JQuery provides two extensions for writing plug-ins.
$. Fn. extend ({}); used to expand the first type
$. Extend ({}); used to expand the second type
The following is the implementation result and code
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head> <title> </title>
<Style type = "text/css">
Li {border: 1px solid #000; cursor: pointer; width: 200px; display: block ;}
</Style>
<Script src = "../Scripts/jquery-1.4.1.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
(Function ($ ){
$. Fn. extend ({
"ChgSC": function (options ){
Options = $. extend ({FontSize: "100px", Color: "red"}, options); // The $. extend method is used to expand an object.
Return this. hover (function () {// return to maintain jQuery's chained operation
Certificate (this).css ({"fontSize": options. FontSize, "color": options. Color });
}, Function (){
Certificate (this).css ({"fontSize": "", "color ":""});
});
}
});
$. Extend ({
"UrlParam": function (){
Var pageUrl = location. search;
If (pageUrl! = "")
Return pageUrl. slice (1 );
Else
Return "No parameter ";
}
});
}) (JQuery );
$ (Function (){
$ ("Li"). chgSC ({FontSize: "130px "});
Alert ($. urlParam ());
});
</Script>
</Head>
<Body>
<Ul>
<Li> 1 </li>
<Li> 2 </li>
<Li> 3 </li>
<Li> 4 </li>
<Li> 5 </li>
</Ul>
</Body>
</Html>