====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