-
Notifications
You must be signed in to change notification settings - Fork 23
/
105_jQuery插件.html
334 lines (302 loc) · 10.7 KB
/
105_jQuery插件.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<pre>
插件开发
全局函数(方法)
全局函数属于jQuery,通过$/jQuery直接调用->$.each(array,fn);
对象函数(方法)
对象函数属于jQuery对象,通过对象调用->$a.val();
添加新的全局函数
添加一个myFunction函数,通过$.myFn();调用
添加方式
$.myFn=function(){};->不推荐
jQuery.myFn=function(){};->不推荐
$.extend({
myFn:function{},
myFn2:function{},
});->推荐($有时会被其他库占用,所以更推荐下面的方式)
$.extend用法
$.extend(obj1,obj2);->将obj2的属性覆盖到obj1上,返回obj1
$.extend(obj1,obj2,obj3);->将obj2、obj3的属性按顺序覆盖到obj1上,返回obj1
$.extend(obj1);->将obj1的属性覆盖到jQuery上
jQuery.extend({
myFn:function{},
myFn2:function{},
});->强烈推荐
命名冲突
直接在jQuery上添加方法可能会出现命名冲突的问题。
将方法封装到一个对象上,再添加到jQuery上
jQuery.myPlugin = {
myFn: function(){}
};
$.myPlugin.myFn();
或
jQuery.extend({
myPlugin: {
myFn: function() {}
}
});
$.myPlugin.myFn();
添加新的对象方法(一般制作插件都用这)
添加一个myFn函数,通过$a.myFn();调用
添加方式
jQuery.fn.myFn=function(){
// myFn中的this是指向调用者(被jQuery选中的元素)
};->常用
// 或
jQuery.fn.extend({
myFn:function(){
// myFn中的this是指向调用者(被jQuery选中的元素)
}
});->不常用
$("a").myFn();
对象方法中的this
对象方法中this指的是调用的jQuery对象
$(".test").myPlugin();
此时
jQuery.fn.myPlugin=function(){
// myPlugin中的this指向的是$(".test")这个jQuery对象
var that=this;
};
注意,如果需要针对jQuery对象中的每个DOM元素进行操作,可使用$obj.each
jQUery.fn.myPlugin=function(){
var that=this;
that.each(function(i,value){
// each中的this指向的是外层jQuery对象(that)中的每个DOM对象,注意是DOM对象,必须$(this)来调用jQuery方法
$(this).toggleClass();
});
};
支持链式调用
jQuery.fn.extend({
myFn:function(){
// 返回一个jquery对象
return $obj;
}
});
接收参数
jQuery.fn.myFn=function(options){
var defaults={};// 默认参数
var settings=jQuery.extend(defaults,options);// 合并参数,defaults会被改变,这样并不好
};
保护好默认参数
jQuery.fn.myFn=function(options){
var defaults={};// 默认参数
var settings=jQuery.extend({},defaults,options);// 合并参数,defaults不会被改变,可用在其他地方
};
面向对象的插件开发
将方法和属性封装到一个对象上,方便管理。
var Test=function(){};
Test.prototype={
constructor:Test,
fn:function(){xxx}
};
jQuery.fn.myPlugin=function(options){
test=new Test(this,options);
return test.fn();
};
用闭包保护插件,并传入需要的变量
(function(){
var Test=function(){};
Test.prototype={
constructor:Test,
fn:function(){xxx}
};
jQuery.fn.myPlugin=function(options){
test=new Test(this,options);
return test.fn();
};
})();
用分号保证插件在任何情况下都能用
;(function(){
xxxxx
})();
</pre>
<div>一项主</div>
<div>请点击营养我会正常地</div>
<ul>
<li>
<a href="http://www.baidu.com">我的微博</a>
</li>
<li>
<a href="http://www.baidu.com">我的博客</a>
</li>
<li>
<a href="http://www.baidu.com">我的小站</a>
</li>
</ul>
<script src="js/jquery-1.12.4.min.js"></script>
<script>
// 添加全局方法
// 第一种,直接在jQuery上添加
// $.myFn = function() {
// console.log("我是Fn");
// };
// $.myFn();
// // 或
// jQuery.myFn2 = function() {
// console.log("我是Fn2");
// };
// $.myFn2();
// 第二种,用extend
// jQuery.extend({
// myFn: function() {
// console.log("haha");
// }
// });
// $.myFn();
// // 或
// jQuery.extend({
// myFn: function() {
// console.log("haha");
// }
// });
// $.myFn();
// 命名空间
// jQuery.myPlugin = {
// myFn: function() {
// console.log("我是myPlugin下的myFn");
// },
// myFn2: function() {
// console.log("我是myPlugin下的myFn2");
// }
// };
// $.myPlugin.myFn();
// $.myPlugin.myFn2();
// 或
// jQuery.extend({
// myPlugin: {
// myFn: function() {
// console.log("haha");
// }
// }
// });
// $.myPlugin.myFn();
// 添加对象方法
// jQuery.fn.myPlugin = function() {
// //在这里面,this指的是用jQuery选中的元素,是个jQuery对象
// this.css('color', 'red');
// this.each(function() {
// //each内部的this是DOM对象
// //对每个元素进行操作
// $(this).append(' ' + $(this).attr('href'));
// });
// };
// $("a").myPlugin();
// 支持链式调用
// jQuery.fn.myPlugin = function() {
// //在这里面,this指的是用jQuery选中的元素,是个jQuery对象
// this.css('color', 'red');
// return this.each(function() {
// //each内部的this是DOM对象
// //对每个元素进行操作
// $(this).append(' ' + $(this).attr('href'));
// });
// };
// console.log($("a").myPlugin());
// 接收参数
// $.fn.myPlugin = function(options) {
// var defaults = {
// 'color': 'red',
// 'fontSize': '12px'
// };
// var settings = $.extend(defaults, options);
// return this.css({
// 'color': settings.color,
// 'fontSize': settings.fontSize
// });
// };
// $("a").myPlugin({
// 'color': 'pink',
// 'fontSize': '20px'
// });
// 保护好默认参数
// $.fn.myPlugin = function(options) {
// var defaults = {
// 'color': 'red',
// 'fontSize': '12px'
// };
// var settings = $.extend({}, defaults, options);
// console.log(defaults);
// return this.css({
// 'color': settings.color,
// 'fontSize': settings.fontSize
// });
// };
// $("a").myPlugin({
// 'color': 'pink',
// 'fontSize': '20px'
// });
// 面向对象的插件开发
//定义Beautifier的构造函数
// var Beautifier = function(ele, opt) {
// this.$element = ele,
// this.defaults = {
// 'color': 'red',
// 'fontSize': '12px',
// 'textDecoration': 'none'
// },
// this.options = $.extend({}, this.defaults, opt)
// }
// //定义Beautifier的方法
// Beautifier.prototype = {
// beautify: function() {
// return this.$element.css({
// 'color': this.options.color,
// 'fontSize': this.options.fontSize,
// 'textDecoration': this.options.textDecoration
// });
// }
// }
// //在插件中使用Beautifier对象
// $.fn.myPlugin = function(options) {
// //创建Beautifier的实体
// //传入的this是jQuery选中的所有元素
// var beautifier = new Beautifier(this, options);
// //调用其方法
// return beautifier.beautify();
// };
// $('a').myPlugin({
// 'color': '#2C9929',
// 'fontSize': '20px'
// });
// 用闭包保护插件,并传入需要的变量
(function($, window, document, undefined) {
//定义Beautifier的构造函数
var Beautifier = function(ele, opt) {
this.$element = ele;
this.defaults = {
'color': 'red',
'fontSize': '12px',
'textDecoration': 'none'
};
this.options = $.extend({}, this.defaults, opt);
};
//定义Beautifier的方法
Beautifier.prototype = {
beautify: function() {
return this.$element.css({
'color': this.options.color,
'fontSize': this.options.fontSize,
'textDecoration': this.options.textDecoration
});
}
};
//在插件中使用Beautifier对象
$.fn.myPlugin = function(options) {
//创建Beautifier的实体
var beautifier = new Beautifier(this, options);
//调用其方法
return beautifier.beautify();
};
})(jQuery, window, document);
$("a").myPlugin({
'color': 'red'
});
</script>
</body>
</html>