Skip to content

Commit a9d30b5

Browse files
committed
bring about-jquery section into core style
1 parent d31c218 commit a9d30b5

File tree

1 file changed

+64
-35
lines changed

1 file changed

+64
-35
lines changed

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

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ level: beginner
77
This is a basic tutorial, designed to help you get started using jQuery. If you
88
don't have a test page setup yet, start by creating a new HTML page with the
99
following contents:
10-
``` html
10+
```
1111
<!doctype html>
1212
<html>
1313
<head>
@@ -18,7 +18,7 @@ following contents:
1818
<a href="http://jquery.com/">jQuery</a>
1919
<script src="jquery.js"></script>
2020
<script>
21-
var foo = "bar";
21+
var foo = "bar";
2222
</script>
2323
</body>
2424
</html>
@@ -27,46 +27,61 @@ following contents:
2727
Edit the `src` attribute in the script tag to point to your copy of jquery.js.
2828
For example, if jquery.js is in the same directory as your HTML file, you
2929
can use:
30-
``` html
30+
```
3131
<script src="jquery.js"></script>
3232
```
3333

34-
You can download your own copy of jQuery from the [Downloading jQuery](../downloading-jquery/) page
34+
You can download your own copy of jQuery from the [Downloading jQuery](http://jquery.com/download/) page
3535

3636
### Launching Code on Document Ready
3737
The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:
38-
``` js
39-
window.onload = function(){ alert("welcome"); }
38+
```
39+
window.onload = function() {
40+
41+
alert("welcome");
42+
43+
}
4044
```
4145
Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is that the HTML 'document' isn't finished loading yet, when you first try to run your code.
4246

4347
To circumvent both problems, jQuery has a simple statement that checks the `document` and waits until it's ready to be manipulated, known as the [ ready event ](http://api.jquery.com/ready):
4448

45-
``` js
46-
$(document).ready(function(){
49+
```
50+
$( document ).ready( function() {
51+
4752
// Your code here
53+
4854
});
4955
```
5056

5157
Inside the ready event, add a click handler to the link:
5258

53-
``` js
54-
$(document).ready(function(){
55-
$("a").click(function(event){
59+
```
60+
$( document ).ready(function(){
61+
62+
$("a").click(function( event ) {
63+
5664
alert("Thanks for visiting!");
65+
5766
});
67+
5868
});
5969
```
6070
Save your HTML file and reload the test page in your browser. Clicking the link on the page should make a browser's alert pop-up, before leaving to go to the main jQuery page.
6171

6272
For click and most other [events](http://api.jquery.com/category/events/), you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler:
6373

64-
``` js
65-
$(document).ready(function(){
66-
$("a").click(function(<b>event</b>){
74+
```
75+
$( document ).ready(function() {
76+
77+
$("a").click(function( event ) {
78+
6779
alert("As you can see, the link no longer took you to jquery.com");
68-
<b>event.preventDefault();</b>
80+
81+
event.preventDefault();
82+
6983
});
84+
7085
});
7186
```
7287

@@ -79,7 +94,7 @@ Also, while the custom script is included in the `<head>`, it is generally
7994
preferable to place it in a separate file and refer that file with the script
8095
element's src attribute
8196

82-
``` html
97+
```
8398
<!DOCTYPE html>
8499
<html lang="en">
85100
<head>
@@ -90,12 +105,19 @@ element's src attribute
90105
<a href="http://jquery.com/">jQuery</a>
91106
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
92107
<script>
93-
$(document).ready(function(){
94-
$("a").click(function(event){
108+
109+
$(document).ready(function() {
110+
111+
$("a").click(function( event ) {
112+
95113
alert("The link will no longer take you to jquery.com");
114+
96115
event.preventDefault();
116+
97117
});
118+
98119
});
120+
99121
</script>
100122
</body>
101123
</html>
@@ -109,23 +131,25 @@ Another common task is adding (or removing) a `class`.
109131

110132
First, add some style information into the `head` of your document, like this:
111133

112-
``` html
134+
```
113135
<style>
114-
a.test { font-weight: bold; }
136+
a.test {
137+
font-weight: bold;
138+
}
115139
</style>
116140
```
117141

118142
Next, add the [addClass](http://api.jquery.com/addClass) call to your script:
119143

120-
``` js
144+
```
121145
$("a").addClass("test");
122146
```
123147

124148
All your `a` elements will now be bold.
125149

126150
To remove the `class`, use [removeClass](http://api.jquery.com/removeClass)
127151

128-
``` js
152+
```
129153
$("a").removeClass("test");
130154
```
131155

@@ -135,10 +159,13 @@ In jQuery, a couple of handy [effects](http://api.jquery.com/category/effects/)
135159
are provided, to really make your web site stand out. To put this to the test,
136160
change the click that you added earlier to this:
137161

138-
``` js
139-
$("a").click(function(event){
162+
```
163+
$("a").click(function( event ){
164+
140165
event.preventDefault();
141-
$(this).hide("slow");
166+
167+
$( this ).hide("slow");
168+
142169
});
143170
```
144171

@@ -156,8 +183,8 @@ the callback.
156183

157184
For a callback with no arguments you pass it like this:
158185

159-
``` js
160-
$.get('myhtmlpage.html', myCallBack);
186+
```
187+
$.get( "myhtmlpage.html", myCallBack );
161188
```
162189

163190
**Note** that the second parameter here is simply the function name (but *not* as a string and without parentheses). Functions in Javascript are 'First class citizens' and so can be passed around like variable references and executed at a later time.
@@ -169,30 +196,32 @@ $.get('myhtmlpage.html', myCallBack);
169196
#### Wrong
170197
The Wrong Way (will ***not*** work!)
171198

172-
``` js
173-
$.get('myhtmlpage.html', myCallBack(param1, param2));
199+
```
200+
$.get( "myhtmlpage.html", myCallBack(param1, param2) );
174201
```
175202

176203

177-
This will not work because it calls `myCallBack(param1, param2)`
204+
This will not work because it calls `myCallBack( param1, param2 )`
178205

179206

180207
and then passes the return value as the second parameter to [$.get()](http://api.jquery.com/jQuery.get/)
181208

182209
#### Right
183210

184-
The problem with the above example is that `myCallBack(param1, param2)` is
211+
The problem with the above example is that `myCallBack( param1, param2 )` is
185212
evaluated before being passed as a function. Javascript and by extension jQuery
186213
expects a function pointer in cases like these, e.g., `setTimeout( function() {}, 100)`
187214

188215
In the below usage, an anonymous function is created (just a block of
189216
statements) and is registered as the callback function. Note the use of
190-
`function(){`. The anonymous function does exactly one thing: calls
217+
`function() {`. The anonymous function does exactly one thing: calls
191218
`myCallBack`, with the values of `param1` and `param2` from the outer scope.
192219

193-
``` js
194-
$.get('myhtmlpage.html', function(){
195-
myCallBack(param1, param2);
220+
```
221+
$.get( "myhtmlpage.html", function() {
222+
223+
myCallBack( param1, param2 );
224+
196225
});
197226
```
198227

0 commit comments

Comments
 (0)