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: page/plugins/basic-plugin-creation.md
+13-34
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title : How to Create a Basic Plugin
3
3
level: intermediate
4
4
source: http://jqfundamentals.com/legacy
5
-
attribution:
5
+
attribution:
6
6
- jQuery Fundamentals
7
7
---
8
8
Sometimes you want to make a piece of functionality available throughout your code;
@@ -19,7 +19,7 @@ $("a").css( "color", "red" );
19
19
20
20
This is some pretty basic jQuery code, but do you know what's happening behind the scenes? Whenever you use the `$` function to select elements, it returns a jQuery object. This object contains all of the methods you've been using (`css()`, `click()`, etc.), and all of the elements that fit your selector. The jQuery object gets these methods from the `$.fn` object. This object contains all of the jQuery object methods, and if we want to write our own methods, it will need to contain those as well.
21
21
22
-
Additionally the jQuery utility method `$.trim()` is used above to remove any leading or trailing empty space characters from the user input. Utility methods are functions that reside directly on
22
+
Additionally the jQuery utility method `$.trim()` is used above to remove any leading or trailing empty space characters from the user input. Utility methods are functions that reside directly on
23
23
`$` function itself. You may occasionally want to write a utility method plugin when your extension to the jQuery API does not have to do something to a set of DOM elements you've retrieved.
24
24
25
25
##Basic Plugin Authoring
@@ -29,7 +29,7 @@ Let's say we want to create a plugin that makes text within a set of retrieved e
29
29
```
30
30
$.fn.greenify = function() {
31
31
this.css( "color", "green" );
32
-
}
32
+
};
33
33
34
34
$("a").greenify(); // makes all the links green
35
35
```
@@ -60,14 +60,14 @@ The `$` variable is very popular among JavaScript libraries, and if you're using
60
60
$.fn.greenify = function() {
61
61
this.css( "color", "green" );
62
62
return this;
63
-
}
63
+
};
64
64
65
65
$.ltrim = function( str ) {
66
66
return str.replace(/^\s+/, '');
67
-
}
67
+
};
68
68
$.rtrim = function( str ) {
69
69
return str.replace(/\s+$/, '');
70
-
}
70
+
};
71
71
72
72
}( jQuery ));
73
73
```
@@ -81,7 +81,7 @@ In addition, the primary purpose of an Immediately Invoked Function is to allow
81
81
$.fn.greenify = function() {
82
82
this.css( "color", shade );
83
83
return this;
84
-
}
84
+
};
85
85
86
86
}( jQuery ));
87
87
```
@@ -110,35 +110,25 @@ It would be much better to have one slot, and use parameters to control what act
110
110
(function( $ ) {
111
111
112
112
$.fn.popup = function( action ) {
113
-
114
113
if ( action === "open") {
115
-
116
114
// Open popup code
117
-
118
115
} if ( action === "close" ) {
119
-
120
116
// Close popup code
121
-
122
117
}
123
-
124
118
};
125
-
}( jQuery ));
119
+
}( jQuery ));
126
120
```
127
121
128
-
## Using the `each()` Method
129
-
Your typical jQuery object will contain references to any number of DOM elements, and that's why jQuery objects are often referred to as collections.
122
+
## Using the `each()` Method
123
+
Your typical jQuery object will contain references to any number of DOM elements, and that's why jQuery objects are often referred to as collections.
130
124
If you want to do any manipulating with specific elements (e.g. getting data an attribute, calculating specific positions) then you need to use `each()` to
131
-
loop through the elements.
125
+
loop through the elements.
132
126
133
127
```
134
128
$.fn.myNewPlugin = function() {
135
-
136
129
return this.each(function() {
137
-
138
130
// do something to each element here
139
-
140
131
});
141
-
142
132
};
143
133
```
144
134
@@ -155,8 +145,7 @@ accept some options.
155
145
156
146
```
157
147
(function ( $ ) {
158
-
159
-
$.greenify = function( options ) {
148
+
$.fn.greenify = function( options ) {
160
149
161
150
// This is the easiest way to have default options.
162
151
var settings = $.extend({
@@ -192,15 +181,10 @@ we've discussed:
192
181
```
193
182
(function( $ ){
194
183
$.fn.showLinkLocation = function() {
195
-
196
184
return this.filter("a").each(function() {
197
-
198
185
$( this ).append( " (" + $( this ).attr("href") + ")" );
199
-
200
186
});
201
-
202
-
};
203
-
187
+
};
204
188
}( jQuery ));
205
189
206
190
// Usage example:
@@ -222,16 +206,11 @@ Our plugin can be optimized though:
0 commit comments