-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathindex.html
387 lines (351 loc) · 12 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JS201: JS and the DOM (Review)</title>
<base target="_blank">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="../common/css/reveal.min.css">
<link rel="stylesheet" href="../common/css/gdilight.css" id="theme">
<!-- For syntax highlighting -->
<!-- dark editor -->
<link rel="stylesheet" href="../common/css/editor/dark.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="../common/css/print/pdf.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="../common/favicon.ico" />
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<style type="text/css">
button {
height:44px;
font-size:24px;
margin: 24px;
}
img.treeExample {
width: 45%;
float: left;
}
pre.treeExample {
width: 45%;
margin-left: 5%;
float: left;
font-size: 1rem;
}
.reveal pre code {
box-shadow: none;
display: block;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<br>
<h1 class="upper">JS201: JS and the DOM (Review)</h1>
<br>
<p>
<small>Slides: <a href="http://www.teaching-materials.org/jsdom" class="roll"><span data-title="http://www.teaching-materials.org/jsdom">http://www.teaching-materials.org/jsdom</span></a></small>
</p>
</section>
<section>
<h1>What is the DOM?</h1>
<h2>"Document Object Model"</h2>
<center><img src="http://dl.dropbox.com/u/10998095/Screenshots/2dvqiz-0w2cx.png"></center>
</section>
<section>
<h3>Terminology</h3>
<br>
<ul>
<li>The DOM, just like JavaScript objects, is a data structure called a "tree"</li>
<li>Each point of data is called a "node"</li>
<li>Each "node" can have a "parent", "child", and/or "sibling" nodes</li>
<li>The DOM is accessed by a global variable called <code>document</code></li>
<li>We can also use "dot notation" to access methods and properties</li>
</ul>
</section>
<section>
<h3>Try it!</h3>
<br>
<p>Work with a buddy. Look at the image and code on the next slide and take turns identifying each of the following:</p>
<br>
<center>
<table>
<tr>
<td>parent + child</td>
<td>sibling + sibling</td>
<td>node</td>
<td>tag</td>
<td>attribute</td>
</tr>
</table>
</center>
</section>
<section>
<center>
<img class="treeExample" src="https://dl.dropboxusercontent.com/u/17585217/gdisf/tree.png">
<pre class="treeExample"><code><html>
<head>
<title>My page</title>
</head>
<body>
<h1>This is a header - the biggest one</h1>
<p>This is a paragraph</p>
<ul>
<li>This is a list item</li>
<li>And here is another</li>
<li>Fish</li>
</ul>
<div class="catbox">
<img src="http://placekitten.com/300/300" id="ms_von_cuten">
<p>Ms Von Cuten</p>
</div>
<div class="catbox">
<img src="http://placekitten.com/200/400" id="mr_snuggles">
<p>Mister Snuggles </p>
</div>
<div class="catbox">
<img src="http://placekitten.com/300/500" id="professor_buttonsworth">
<p>Professor Buttonsworth</p>
</div>
</body>
</html>
</code></pre>
</center>
</section>
<section>
<h3>One simple way to visualize it...</h3>
<pre><code>document = {
head: {
children: [ ] //returns child nodes
},
body: {
classList: ['reveal'],
hasChildNodes: function() {
//returns true or false
}
},
querySelectorAll: function(arg) {
//returns matching node list
}
}
</code></pre>
<br>
<p>Because the document is an object, we can access properties and methods with "dot notation".</p>
</section>
<section>
<h3>Try it!</h3>
<br>
<p>Open your JS console and try these...</p>
<pre><code>document.body</code></pre>
<pre><code>document.body.children</code></pre>
<pre><code>document.querySelectorAll('section')</code></pre>
<br>
<p>Can you find any other methods and properties this way?</p>
</section>
</section>
<section>
<h1>Traversing the DOM</h1>
<br>
<h2>Sometimes we need to find the right node</h2>
<ul>
<li>animate</li>
<li>react/listen</li>
<li>get/set data</li>
</ul>
</section>
<section>
<h2>Selecting a list of nodes</h2>
<br>
<p>by selector</p>
<pre><code>document.querySelectorAll('section h2');</code></pre>
<br>
<p>by tag name</p>
<pre><code>document.getElementsByTagName('div');</code></pre>
<br>
<p>by class name</p>
<pre><code>document.getElementsByClassName('upper');</code></pre>
</section>
<section id="section">
<h2>Selecting a single node</h2>
<br>
<p>by ID</p>
<pre><code>document.getElementById('section');</code></pre>
<br>
<p>by selector</p>
<pre><code>document.querySelectorAll('section h2')[3];</code></pre>
<br>
<p>by tag name</p>
<pre><code>document.getElementsByTagName('p')[9];</code></pre>
<br>
<p>by class name</p>
<pre><code>document.getElementsByClassName('upper')[0];</code></pre>
</section>
<section>
<h3>Try it!</h3>
<ul>
<li>In your browser, navigate to <a href="https://twitter.com/gdisf" target="_blank">https://twitter.com/gdisf</a></li>
<li>Work with a buddy. One person will verbally call out an element on the page. The other will select it using the JS console. Take turns switching!</li>
<li>You will know you got it right when you hover over the element in the console and the element highlights on the page.</li>
</ul>
</section>
<section>
<h2>Creating a node</h2>
<center><img src="https://dl.dropboxusercontent.com/u/17585217/gdisf/DOM_3step_process2.png"/></center>
</section>
<section>
<h3>Three step process</h3>
<br>
<h4>Create and store</h4>
<pre><code>var myNewElement = document.createElement('div');</code></pre>
<h4>Edit</h4>
<pre><code>myNewElement.innerHTML = 'I am the html inside of a <em>brand new</em> div tag!';
myNewElement.className = 'new';</code></pre>
<h4>Insert</h4>
<pre><code>document.body.appendChild(myNewElement);</code></pre>
</section>
<section>
<h3>Try it!</h3>
<p>Using the Developer Tools Console, create an <code><img></code> tag and store it. Give it an <code>src</code> attribute with an image of your choice. Finally, append it to this slide!</p>
</section>
<section>
<h2>Events and their Callbacks</h2>
</section>
<section>
<h3>Terminology</h3>
<br>
<ul>
<li><strong>Event:</strong> something that happens</li>
<li><strong>Callback:</strong> a function that executes after the event has happened</li>
<li><strong>Event listener:</strong> a method that binds an event to a callback</li>
</ul>
</section>
<section>
<h3>A basic example</h3>
<p>Try this in your console</p>
<pre><code>var myButton = document.querySelectorAll('button#testButton')[0];
myButton.addEventListener('click', function() {
console.log('button clicked!');
});</code></pre>
<button id="testButton">< button id="testButton" > Click me! < /button ></button>
</section>
<section>
<h3>Try it!</h3>
<br>
<p>In your console, update the last event listener and callback so that it logs a count of how many times the button has been clicked. </p>
</section>
<section>
<h3>Anonymous functions</h3>
<br>
<p>Example:</p>
<pre><code>var myButton = document.querySelectorAll('button#testButton')[0];
myButton.addEventListener('click', function() {
console.log('button clicked!');
});</code></pre>
<br>
<p>The callback cannot be removed, because we don't have a reference to it.</p>
</section>
<section>
<h3>Referenced functions</h3>
<p>Example:</p>
<pre><code>var myButton = document.querySelectorAll('button#testButton')[0];
//store and define the function
var myCallback = function() {
console.log('button clicked!');
};
myButton.addEventListener('click', myCallback);
</code></pre>
<br>
<p>The callback can be removed, since we have a reference.</p>
<pre><code>myButton.removeEventListener('click', myCallback)</code></pre>
</section>
<section>
<h3>What about my arguments?</h3>
<br>
<p>Arguments like the Event Object get applied to the callback:</p>
<pre><code>var myButton = document.querySelectorAll('button#testButton')[0];
//store and define the function
var myCallback = function(e) {
console.log(e); //logs the Event object
};
//no argument needed for binding the event
myButton.addEventListener('click', myCallback);
</code></pre>
</section>
<section>
<h2><a href="exercises/instructions.html" target="_blank">Final Exercise</a></h2>
</section>
<section>
<br>
<h1 class="upper">Appendix</h1>
<h2>Advanced Features of Functions</h2>
</section>
<section>
<h3>How do arguments get applied?</h3>
<br>
<h4>Part 1: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments" target="_blank">arguments array</a></h4>
<p>Each JavaScript function has an <code>arguments</code> array, regardless of how you define it.</p>
<pre><code>// notice no arguments here
var myFunc1 = function() {
console.log(arguments);
}
// one argument here
var myFunc2 = function(e) {
console.log(e);
console.log(arguments);
}
myFunc1('abc', 123); // logs ['abc', 123]
myFunc2({Event: 'click'}, false); // notice extraneous argument
// logs {Event: 'click'}
// then logs [{Event: 'click'}, false]</code></pre>
</section>
<section>
<h3>How do arguments get applied?</h3>
<br>
<h4>Part 2: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply" target="_blank">.apply()</a></h4>
<p>Each JavaScript function has a method <code>.apply()</code>, which calls a function with a specified scope and arguments array.</p>
<pre><code>var myCallback = function(e) {
console.log(e);
}
var myEventListener = function() {
myCallback.apply(this, arguments);
}
myEventListener('Wow, look at this string get applied as an argument to myCallback!')
</code></pre>
</section>
</div>
<footer>
<div class="copyright">
JS200 - JS DOM -- San Francisco --
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="../common/lib/js/head.min.js"></script>
<script src="../common/js/reveal.min.js"></script>
<script src="../common/js/jquery.min.js"></script>
<script src="../common/js/underscore-min.js"></script>
<script src="../common/js/backbone.js"></script>
<script src="../common/js/handlebars.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
rollingLinks: false,
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: '../common/js/highlight.js', async: true, callback: function() { window.hljs.initHighlightingOnLoad(); } },
{ src: '../common/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: '../common/js/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../common/js/data-markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../common/plugin/speakernotes/client.js', async: true, condition: function() { return window.location.host === 'localhost:1947'; } }
]
});
</script>
</body>
</html>