Write your own jquery plugin simple implementation code _jquery

Source: Internet
Author: User
Here only explains how to write their own plug-ins, as to what functions to achieve, to vary from person to person ... Okay, here we go.
jquery plug-ins are mainly divided into three kinds
1, encapsulating the object method of Plug-ins
2, encapsulating the global function of Plug-ins
3, the extension selector plug-in
Here only the first two kinds of writing, that is more common.
Most plug-ins are written in this form:
Copy Code code as follows:

(function ($) {
/* Place code here * *
}) (JQuery);

The advantage is that the function can still use $ as an alias for jquery without affecting other libraries using $
jquery provides two extensions for writing Plug-ins
$.fn.extend ({}); used to extend the first type of
$.extend ({}); Used to extend the second type of
Below is a screenshot of the implementation effect and the code

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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); Here we use the $.extend method to extend an object
Return This.hover (function () {//return in order to maintain the chain operation of jquery
$ (this). css ({"FontSize": Options. FontSize, "Color": Options. Color});
}, function () {
$ (this). css ({"FontSize": "", "Color": "});
});
}
});
$.extend ({
' Urlparam ': function () {
var pageurl = Location.search;
if (Pageurl!= "")
return Pageurl.slice (1);
Else
Return "no parameters";
}
});
}) (JQuery);
$ (function () {
$ ("Li"). CHGSC ({fontsize: "130px"});
Alert ($.urlparam ());
});
</script>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</body>
Related Article

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.