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/about-jquery/how-jquery-works.md
+49-48Lines changed: 49 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -2,103 +2,110 @@
2
2
title : How jQuery Works
3
3
level: beginner
4
4
---
5
+
5
6
### jQuery: The Basics
6
7
7
-
This is a basic tutorial, designed to help you get started using jQuery. If you
8
-
don't have a test page setup yet, start by creating the following HTML page:
8
+
This is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating the following HTML page:
9
9
10
10
```
11
11
<!doctype html>
12
12
<html>
13
13
<head>
14
-
<meta charset="utf-8">
14
+
<meta charset="utf-8" />
15
15
<title>Demo</title>
16
16
</head>
17
17
<body>
18
18
<a href="http://jquery.com/">jQuery</a>
19
19
<script src="jquery.js"></script>
20
20
<script>
21
-
// Your code goes here
21
+
22
+
// Your code goes here.
23
+
22
24
</script>
23
25
</body>
24
26
</html>
25
27
```
26
28
27
-
The `src` attribute in the `<script>` element must point to a copy of jQuery.
28
-
Download a copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page
29
-
and store the `jquery.js` file in the same directory as your HTML file.
29
+
The `src` attribute in the `<script>` element must point to a copy of jQuery. Download a copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page and store the `jquery.js` file in the same directory as your HTML file.
30
30
31
31
### Launching Code on Document Ready
32
32
33
-
To ensure that their code runs after the browser finishes loading the document,
34
-
many JavaScript programmers wrap their code in an `onload` function:
33
+
To ensure that their code runs after the browser finishes loading the document, many JavaScript programmers wrap their code in an `onload` function:
35
34
36
35
```
37
36
window.onload = function() {
37
+
38
38
alert( "welcome" );
39
+
39
40
}
40
41
```
41
42
42
-
Unfortunately, the code doesn't run until all images are finished downloading, including banner ads.
43
-
To run code as soon as the `document` is ready to be manipulated, jQuery has a statement
44
-
known as the [ready event](http://api.jquery.com/ready):
43
+
Unfortunately, the code doesn't run until all images are finished downloading, including banner ads. To run code as soon as the document is ready to be manipulated, jQuery has a statement known as the [ready event](http://api.jquery.com/ready/):
45
44
46
45
```
46
+
47
47
$( document ).ready(function() {
48
-
// Your code here
48
+
49
+
// Your code here.
50
+
49
51
});
50
52
```
51
53
52
54
For example, inside the `ready` event, you can add a click handler to the link:
53
55
54
56
```
55
57
$( document ).ready(function() {
58
+
56
59
$( "a" ).click(function( event ) {
60
+
57
61
alert( "Thanks for visiting!" );
62
+
58
63
});
64
+
59
65
});
60
66
```
61
67
62
-
Save your HTML file and reload the test page in your browser.
63
-
Clicking the link should now first display an alert pop-up,
64
-
then continue with the default behavior of navigating to http://jquery.com.
68
+
Save your HTML file and reload the test page in your browser. Clicking the link should now first display an alert pop-up, then continue with the default behavior of navigating to http://jquery.com.
65
69
66
-
For `click` and most other [events](http://api.jquery.com/category/events/),
67
-
you can prevent the default behavior by calling `event.preventDefault()` in the event handler:
70
+
For `click` and most other [events](http://api.jquery.com/category/events/), you can prevent the default behavior by calling `event.preventDefault()` in the event handler:
68
71
69
72
```
70
73
$( document ).ready(function() {
74
+
71
75
$( "a" ).click(function( event ) {
76
+
72
77
alert( "As you can see, the link no longer took you to jquery.com" );
78
+
73
79
event.preventDefault();
80
+
74
81
});
82
+
75
83
});
76
84
```
77
85
78
86
### Complete Example
79
87
80
-
The following example illustrates the click handling code discussed above,
81
-
embedded directly in the HTML `<body>`. Note that in practice,
82
-
it is usually better to place your code in a separate JS file
83
-
and load it on the page with a `<script>` element's `src` attribute.
88
+
The following example illustrates the click handling code discussed above, embedded directly in the HTML `<body>`. Note that in practice, it is usually better to place your code in a separate JS file and load it on the page with a `<script>` element's `src` attribute.
84
89
85
90
```
86
91
<!doctype html>
87
92
<html>
88
93
<head>
89
-
<meta charset="utf-8">
94
+
<meta charset="utf-8" />
90
95
<title>Demo</title>
91
96
</head>
92
97
<body>
93
98
<a href="http://jquery.com/">jQuery</a>
94
99
<script src="jquery.js"></script>
95
100
<script>
101
+
96
102
$( document ).ready(function() {
97
103
$( "a" ).click(function( event ) {
98
104
alert( "The link will no longer take you to jquery.com" );
99
105
event.preventDefault();
100
106
});
101
107
});
108
+
102
109
</script>
103
110
</body>
104
111
</html>
@@ -108,7 +115,7 @@ and load it on the page with a `<script>` element's `src` attribute.
108
115
109
116
**Important:***You must place the remaining jQuery examples inside the `ready` event so that your code executes when the document is ready to be worked on.*
110
117
111
-
Another common task is adding or removing a `class`.
118
+
Another common task is adding or removing a class.
112
119
113
120
First, add some style information into the `<head>` of the document, like this:
114
121
@@ -120,42 +127,39 @@ a.test {
120
127
</style>
121
128
```
122
129
123
-
Next, add the [addClass()](http://api.jquery.com/addClass) call to the script:
130
+
Next, add the [.addClass()](http://api.jquery.com/addClass/) call to the script:
124
131
125
132
```
126
133
$( "a" ).addClass( "test" );
127
134
```
128
135
129
-
All `a` elements are now bold.
136
+
All `<a>` elements are now bold.
130
137
131
-
To remove an existing `class`, use [removeClass()](http://api.jquery.com/removeClass):
138
+
To remove an existing class, use [.removeClass()](http://api.jquery.com/removeClass/):
132
139
133
140
```
134
141
$( "a" ).removeClass( "test" );
135
142
```
136
143
137
144
### Special Effects
138
145
139
-
jQuery also provides some handy [effects](http://api.jquery.com/category/effects/)
140
-
to help you make your web sites stand out.
141
-
For example, if you create a click handler of:
146
+
jQuery also provides some handy [effects](http://api.jquery.com/category/effects/) to help you make your web sites stand out. For example, if you create a click handler of:
142
147
143
148
```
144
-
$( "a" ).click(function( event ){
149
+
$( "a" ).click(function( event ) {
150
+
145
151
event.preventDefault();
152
+
146
153
$( this ).hide( "slow" );
154
+
147
155
});
148
156
```
149
157
150
-
then the link slowly disappears when clicked.
158
+
Then the link slowly disappears when clicked.
151
159
152
160
## Callbacks and Functions
153
161
154
-
Unlike many other programming languages, JavaScript enables you to freely pass functions around to be executed at a later time.
155
-
A *callback* is a function that is passed as an argument to another function and
156
-
is executed after its parent function has completed. Callbacks are special because
157
-
they patiently wait to execute until their parent finishes.
158
-
Meanwhile, the browser can be executing other functions or doing all sorts of other work.
162
+
Unlike many other programming languages, JavaScript enables you to freely pass functions around to be executed at a later time. A *callback* is a function that is passed as an argument to another function and is executed after its parent function has completed. Callbacks are special because they patiently wait to execute until their parent finishes. Meanwhile, the browser can be executing other functions or doing all sorts of other work.
159
163
160
164
To use callbacks, it is important to know how to pass them into their parent function.
161
165
@@ -167,8 +171,9 @@ If a callback has no arguments, you can pass it in like this:
167
171
$.get( "myhtmlpage.html", myCallBack );
168
172
```
169
173
170
-
When [$.get](http://api.jquery.com/jQuery.get/) finishes getting the page `myhtmlpage.html`, it executes the `myCallBack` function.
171
-
**Note** that the second parameter here is simply the function name (but *not* as a string and without parentheses).
174
+
When [$.get()](http://api.jquery.com/jQuery.get/) finishes getting the page `myhtmlpage.html`, it executes the `myCallBack()` function.
175
+
176
+
***Note:** The second parameter here is simply the function name (but *not* as a string, and without parentheses).
172
177
173
178
### Callback *with* Arguments
174
179
@@ -181,22 +186,18 @@ This code example will ***not*** work:
The reason this fails is that the code executes `myCallBack( param1, param2 )` immediately
185
-
and then passes myCallBack's *return value* as the second parameter to `$.get`.
186
-
We actually want to pass the function `myCallBack`, not `myCallBack( param1, param2 )`'s return value
187
-
(which might or might not be a function). So, how to pass in `myCallBack`*and* include its arguments?
189
+
The reason this fails is that the code executes `myCallBack( param1, param2 )` immediately and then passes `myCallBack()`'s *return value* as the second parameter to `$.get()`. We actually want to pass the function `myCallBack()`, not `myCallBack( param1, param2 )`'s return value (which might or might not be a function). So, how to pass in `myCallBack()`*and* include its arguments?
188
190
189
191
#### Right
190
192
191
-
To defer executing `myCallBack` with its parameters, you can use an anonymous function as a wrapper.
192
-
Note the use of `function() {`. The anonymous function does exactly one thing: calls
193
-
`myCallBack`, with the values of `param1` and `param2`.
193
+
To defer executing `myCallBack()` with its parameters, you can use an anonymous function as a wrapper. Note the use of `function() {`. The anonymous function does exactly one thing: calls `myCallBack()`, with the values of `param1` and `param2`.
194
194
195
195
```
196
196
$.get( "myhtmlpage.html", function() {
197
+
197
198
myCallBack( param1, param2 );
199
+
198
200
});
199
201
```
200
202
201
-
When `$.get` finishes getting the page `myhtmlpage.html`, it executes the anonymous function,
202
-
which executes `myCallBack( param1, param2 )`.
203
+
When `$.get()` finishes getting the page `myhtmlpage.html`, it executes the anonymous function, which executes `myCallBack( param1, param2 )`.
0 commit comments