Skip to content

Commit b1ed6d3

Browse files
Markus Amalthea Magnusonajpiano
Markus Amalthea Magnuson
authored andcommitted
Various small fixes on the How jQuery Works page. Fixes jquery#342.
1 parent 59d5078 commit b1ed6d3

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

page/about-jquery/how-jquery-works.md

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,110 @@
22
title : How jQuery Works
33
level: beginner
44
---
5+
56
### jQuery: The Basics
67

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:
99

1010
```
1111
<!doctype html>
1212
<html>
1313
<head>
14-
<meta charset="utf-8">
14+
<meta charset="utf-8" />
1515
<title>Demo</title>
1616
</head>
1717
<body>
1818
<a href="http://jquery.com/">jQuery</a>
1919
<script src="jquery.js"></script>
2020
<script>
21-
// Your code goes here
21+
22+
// Your code goes here.
23+
2224
</script>
2325
</body>
2426
</html>
2527
```
2628

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.
3030

3131
### Launching Code on Document Ready
3232

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:
3534

3635
```
3736
window.onload = function() {
37+
3838
alert( "welcome" );
39+
3940
}
4041
```
4142

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/):
4544

4645
```
46+
4747
$( document ).ready(function() {
48-
// Your code here
48+
49+
// Your code here.
50+
4951
});
5052
```
5153

5254
For example, inside the `ready` event, you can add a click handler to the link:
5355

5456
```
5557
$( document ).ready(function() {
58+
5659
$( "a" ).click(function( event ) {
60+
5761
alert( "Thanks for visiting!" );
62+
5863
});
64+
5965
});
6066
```
6167

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.
6569

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:
6871

6972
```
7073
$( document ).ready(function() {
74+
7175
$( "a" ).click(function( event ) {
76+
7277
alert( "As you can see, the link no longer took you to jquery.com" );
78+
7379
event.preventDefault();
80+
7481
});
82+
7583
});
7684
```
7785

7886
### Complete Example
7987

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.
8489

8590
```
8691
<!doctype html>
8792
<html>
8893
<head>
89-
<meta charset="utf-8">
94+
<meta charset="utf-8" />
9095
<title>Demo</title>
9196
</head>
9297
<body>
9398
<a href="http://jquery.com/">jQuery</a>
9499
<script src="jquery.js"></script>
95100
<script>
101+
96102
$( document ).ready(function() {
97103
$( "a" ).click(function( event ) {
98104
alert( "The link will no longer take you to jquery.com" );
99105
event.preventDefault();
100106
});
101107
});
108+
102109
</script>
103110
</body>
104111
</html>
@@ -108,7 +115,7 @@ and load it on the page with a `<script>` element's `src` attribute.
108115

109116
**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.*
110117

111-
Another common task is adding or removing a `class`.
118+
Another common task is adding or removing a class.
112119

113120
First, add some style information into the `<head>` of the document, like this:
114121

@@ -120,42 +127,39 @@ a.test {
120127
</style>
121128
```
122129

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:
124131

125132
```
126133
$( "a" ).addClass( "test" );
127134
```
128135

129-
All `a` elements are now bold.
136+
All `<a>` elements are now bold.
130137

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/):
132139

133140
```
134141
$( "a" ).removeClass( "test" );
135142
```
136143

137144
### Special Effects
138145

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:
142147

143148
```
144-
$( "a" ).click(function( event ){
149+
$( "a" ).click(function( event ) {
150+
145151
event.preventDefault();
152+
146153
$( this ).hide( "slow" );
154+
147155
});
148156
```
149157

150-
then the link slowly disappears when clicked.
158+
Then the link slowly disappears when clicked.
151159

152160
## Callbacks and Functions
153161

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.
159163

160164
To use callbacks, it is important to know how to pass them into their parent function.
161165

@@ -167,8 +171,9 @@ If a callback has no arguments, you can pass it in like this:
167171
$.get( "myhtmlpage.html", myCallBack );
168172
```
169173

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).
172177

173178
### Callback *with* Arguments
174179

@@ -181,22 +186,18 @@ This code example will ***not*** work:
181186
$.get( "myhtmlpage.html", myCallBack( param1, param2 ) );
182187
```
183188

184-
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?
188190

189191
#### Right
190192

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`.
194194

195195
```
196196
$.get( "myhtmlpage.html", function() {
197+
197198
myCallBack( param1, param2 );
199+
198200
});
199201
```
200202

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

Comments
 (0)