Powerful jquery-Custom Plugins

Source: Internet
Author: User

====jquery Plugin Authoring Principle =====
1. Name jquery.< plug-in name >.js
2. Inside the plug-in, this refers to the jquery object made by the current selector, not an internal object, such as Click (), and the internal this point is the DOM element
3.this.each can traverse all elements
4. Add a semicolon to the plugin head to prevent problems when compressing
5. Use closure notation

;(function ($) {//$ is a jquery object
/ code block /
}) (JQuery);

=====jquery.fn.extend () and jquery.extend () difference =======

JQuery.fn.extend () Encapsulates the object method, as an example

<title>extend:color</title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<style>
. a{
color:red;
}
</style>
<script type= "Text/javascript" src= ". /.. /scripts/jquery.js "></script>
<script type= "Text/javascript" >
Plugin Authoring
;(function ($) {
JQuery.fn.extend ({
"Color": function (value) {
return this.css ("Color", value);
}
});
}) (JQuery);

//插件应用$(function(){    //查看第一个div的color样式值    alert($("div").color()+"\n返回字符串,证明此插件可用。");      //把所有的div的字体颜色都设为红色    alert( $("div").color("red")+"\n返回object证明得到的是jQuery对象。");})</script>

<body>
<div class= "a" >red</div>
<div style= "Color:blue" >blue</div>
<div style= "Color:green" >green</div>
<div style= "Color:yellow" >yellow</div>

</body>

Jquery.extend ()

Extending a JQuery object, encapsulating a global function, or a selector plug-in, often used to design a list of default parameters for a plug-in, as shown below

var setting = {A:1,b:2,c:3}
var option = {A:9,b:8}
var Newjson = jquery.extend (setting,option);
The result is Newjson = {A:9,b:8,c:3}

Powerful jquery-Custom Plugins

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.