You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/plugins/basic-plugin-creation.md
+83-79Lines changed: 83 additions & 79 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,75 +4,81 @@ section : 2
4
4
title : How to create a basic plugin
5
5
attribution: jQuery Fundamentals
6
6
---
7
+
Sometimes you want to make a piece of functionality available throughout your code;
8
+
for example, perhaps you want a single method you can call on a jQuery selection that performs a series of operations on the selection.
9
+
In this case, you may want to write a plugin.
10
+
11
+
Most plugins are simply methods created in the `$.fn` namespace. jQuery guarantees that a method called on a jQuery object will be able to access that jQuery object as this inside the method.
12
+
In return, your plugin needs to guarantee that it returns the same object it received, unless explicitly documented otherwise.
13
+
7
14
## How to create a basic plugin
8
15
9
16
The notation for creating a typical plugin is as follows:
10
17
11
-
<divclass="example"markdown="1">
12
-
(function($){
13
-
$.fn.myNewPlugin = function() {
14
-
return this.each(function(){
15
-
// do something
16
-
});
17
-
};
18
-
}(jQuery));
19
-
</div>
18
+
<javascript>
19
+
(function($){
20
+
$.fn.myNewPlugin = function() {
21
+
return this.each(function(){
22
+
// do something
23
+
});
24
+
};
25
+
}(jQuery));
26
+
</javascript>
20
27
21
28
Don't let that confuse you though. The point of a jQuery plugin is to extend
22
29
jQuery's prototype object, and that's what's happening on this line:
23
30
24
-
<divclass="example"markdown="1">
25
-
$.fn.myNewPlugin = function() { //...
26
-
</div>
31
+
<javascript>
32
+
$.fn.myNewPlugin = function() { //...
33
+
</javascript>
27
34
28
35
We wrap this assignment in an immediately-invoked function:
29
-
30
-
<divclass="example"markdown="1">
31
-
(function($){
32
-
//...
33
-
}(jQuery));
34
-
</div>
36
+
<javascript>
37
+
(function($){
38
+
//...
39
+
}(jQuery));
40
+
</javascript>
35
41
36
42
This has the effect of creating a "private" scope that allows us to extend
37
43
jQuery using the dollar symbol without having to risk the possibility that the
38
44
dollar has been overwritten by another library.
39
45
40
46
So our actual plugin, thus far, is this:
41
47
42
-
<divclass="example"markdown="1">
43
-
$.fn.myNewPlugin = function() {
44
-
return this.each(function(){
45
-
// do something
46
-
});
47
-
};
48
-
</div>
48
+
<javascript>
49
+
$.fn.myNewPlugin = function() {
50
+
return this.each(function(){
51
+
// do something
52
+
});
53
+
};
54
+
</javascript>
49
55
50
56
The `this` keyword within the new plugin refers to the jQuery object on which
51
57
the plugin is being called.
52
58
53
-
<divclass="example"markdown="1">
54
-
var somejQueryObject = $('#something');
59
+
<javascript>
60
+
var somejQueryObject = $('#something');
55
61
56
-
$.fn.myNewPlugin = function() {
57
-
alert(this === somejQueryObject);
58
-
};
62
+
$.fn.myNewPlugin = function() {
63
+
alert(this === somejQueryObject);
64
+
};
59
65
60
-
somejQueryObject.myNewPlugin(); // alerts 'true'
61
-
</div>
66
+
somejQueryObject.myNewPlugin(); // alerts 'true'
67
+
</javascript>
62
68
63
69
Your typical jQuery object will contain references to any number of DOM
64
70
elements, and that's why jQuery objects are often referred to as collections.
65
71
66
72
So, to do something with a collection we need to loop through it, which is most
67
73
easily achieved using jQuery's `each()` method:
68
74
69
-
<divclass="example"markdown="1">
70
-
$.fn.myNewPlugin = function() {
71
-
return this.each(function(){
75
+
<javascript>
76
+
$.fn.myNewPlugin = function() {
77
+
return this.each(function(){
72
78
73
-
});
74
-
};
75
-
</div>
79
+
});
80
+
};
81
+
</javascript>
76
82
77
83
jQuery's `each()` method, like most other jQuery methods, returns a jQuery
78
84
object, thus enabling what we've all come to know and love as 'chaining'
@@ -81,43 +87,41 @@ return the this object. Within this loop you can do whatever you want with
81
87
each element. Here's an example of a small plugin using some of the techniques
0 commit comments