forked from estelle/jsintro
/
part_06.html
357 lines (290 loc) · 9.36 KB
/
part_06.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
<!doctype HTML>
<html>
<head>
<meta charset="utf-8" />
<title>Intro to JavaScript: Part 5</title>
<link href="http://fonts.googleapis.com/css?family=Droid+Sans|Droid+Sans+Mono|Tangerine" rel="stylesheet" type="text/css" />
<link href="css.css" rel="stylesheet"/>
<style>
.black span {background-color: #333; color: #ddd; padding: 3px 5px;}
#box {font-size: smaller;}
</style>
</head>
<body>
<div id="topinfo">
</div>
<div id="info">
<p>Press <span class="key">→</span> key to advance.
Zoom in/out: <span class="key">Ctrl or Command</span> + <span class="key">+/-</span>.
</p>
</div>
<div id="presentation">
<div id="presentation-counter"></div>
<div id="slides">
<div class="slide intro">
<header><hgroup>
<h1>Basic JS: VI</h1>
<h2>Estelle Weyl</h2>
<h3>Introducing the DOM</h3>
</hgroup></header>
</div>
</section>
</div>
<div class="slide normal">
<header><h1>Uses of JS in Websites</h1></header>
<section class="content">
<ul>
<li>Interactive Effects</li>
<li>Dynamically modifying web pages</li>
<li>Changing the HTML the browser originally downloaded</li>
</ul>
<p>What we're going to do:</p>
<ul>
<li>identify an element on the page</li>
<li>do something to the element</li>
</ul>
</section>
</div>
<div class="slide normal">
<header><h1>The DOM</h1></header>
<section class="content">
<pre contenteditable><!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>A Basic Web Page</title>
</head>
<body>
<p>
<strong>important text</strong> and
<a href="#">a link</a>.
</p>
</body>
</html></pre>
<small>ancestor | parent | node | textnode | sibling | child | descendent</small>
</section>
</div>
<div class="slide normal">
<header><h1>DOM Tree</h1></header>
<section class="content">
<pre contenteditable class="black">
<span>html</span>
______|____________
| |
<span>head</span> <span>body</span>
______|____ |
| | |
<span>title</span> <span>meta</span> <span>p</span>
| ______|_________
| | | | |
A basic web page <span>strong</span> and <span>a</span> .
| |
| |
important text a link
</pre>
</section>
</div>
<div class="slide normal">
<header><h1>Selecting an element</h1></header>
<section class="content">
<pre contenteditable>
document.getElementBy<strong>Id</strong>();
document.getElement<strong>s</strong>By<strong>TagName</strong>();
document.getElementsBy<strong>ClassName</strong>(); //IE9
document.query<strong>Selector</strong>(); //IE8
document.querySelector<strong>All</strong>(); //IE8
</pre>
</section>
</div>
<div class="slide normal">
<header><h1>Test Page</h1></header>
<section class="content">
<pre contenteditable><!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>A Basic Web Page</title>
</head>
<body
<div id="box">
<ul>
<li>Some <strong>imp text</strong></li>
<li>And <a href="http://x.com">a link</a>.</li>
<li class="cl">With a class</li>
<li class="cl">another classed</li>
<li class="di">Has an ID</li>
</ul>
</div>
</body>
</html></pre>
</section>
</div>
<div class="slide normal">
<header><h1>Play with Page in Console!!!</h1></header>
<section class="content">
<pre contenteditable>
var box = document.getElementById('box');
var box = document.querySelector('#box');
var lis = box.getElementsByTagName('li');
var lis = box.querySelectorAll('li');
var classed = lis.getElementsClassName('cl');
var classed = box.querySelectorAll('.cl');
lis.length?
box.length?
classed.length?
</pre>
<div id="box">
<ul>
<li>Some <strong>imp text</strong></li>
<li>And <a href="http://x.com">a link</a>.</li>
<li class="cl">With a class</li>
<li class="cl">another classed</li>
<li class="di">Has an ID</li>
</ul>
</div>
</section>
</div>
<div class="slide normal">
<header><h1>Child, Parent, Siblings</h1></header>
<section class="content">
<pre contenteditable class="black">
<span>html</span> (parent)
______|____________
| |
<span>head</span> siblings <span>body</span> (children)
</pre>
<pre contenteditable class="black">
<span>p</span> (parent)
______|_________
| | | |
<span>strong</span> and <span>a</span> . (children and siblings)
</pre>
</section>
</div>
<div class="slide normal">
<header><h1>children</h1></header>
<section class="content">
<pre contenteditable>
var box = document.querySelector("#box");
var ul = box.getElementsByTagName("ul")[0];
<small>// includes empty text nodes too</small>
var kids = ul.<strong>childNodes</strong>;
<small>// text if textnode, value if attribute node, null if element</small>
var first = ulKids[0].<strong>nodeValue</strong>;</pre>
<p>Parents</p>
<pre contenteditable>var lis = box.getElementsByTagName('li');
var ul = lis[1].<strong>parentNode</strong>;
</pre>
<p>Siblings</p>
<pre contenteditable>var firstli = lis[1].<strong>prevSibling</strong>;
var thirdli = lis[1].<strong>nextSibling</strong>;</pre>
</section>
</div>
<div class="slide normal">
<header>
<h1>Changing Content</h1>
</header>
<section class="content">
<p>We need to find a DOM node so that we can add content to it, before it, after it, etc.</p>
<pre contenteditable><div id="changeme">
<ul>
<li>Some <strong>imp text</strong></li>
<li>And <a href="http://x.com">a link</a>.</li>
<li class="cl">With a class</li>
<li class="cl">another classed</li>
<li class="di">Has an ID</li>
</ul>
</div></pre>
<pre><small>//find elements</small>
var lis = document.querySelectorAll('#changeme li');
<small>//change the content of one</small>
lis[2].<strong>innerHTML</strong> = 'JavaScript Rocks';</pre>
</section>
</div>
<div class="slide normal">
<header><h1>Change Content</h1></header>
<section class="content">
<div id="changeme">
<ul>
<li>Some <strong>imp text</strong></li>
<li>And <a href="http://x.com">a link</a>.</li>
<li class="cl">With a class</li>
<li class="cl">another classed</li>
<li class="di">Has an ID</li>
</ul>
</div>
<pre><small>//find elements</small>
var lis = document.querySelectorAll('#changeme li');
<small>//change the content of one</small>
lis[2].<strong>innerHTML</strong> = 'JavaScript Rocks';
<small>//find another elements</small>
var strong = lis[0].getElementsByTagName('strong');
<small>//change that content too</small>
strong[0].<strong>innerHTML</strong> = 'emboldened content';
</pre>
</section>
</div>
<div class="slide normal">
<header><h1>Add an element</h1></header>
<section class="content">
<ol>
<li>Create an element</li>
<li>Set attributes on that element</li>
<li>Create your textnode</li>
<li>Add content to that element by appending the textnode to the element you created</li>
<li>Targe the DOM node this new element will be next to</li>
<li>Add that node next to the the targeted node</li>
</ol>
<pre contenteditable>
var newLI = document.<strong>createElement</strong>('li');
newLI.<strong>setAttribute</strong>('class', 'myClass');
var textNode = document.<strong>createTextNode</strong>("some text");
newLI.<strong>appendChild</strong>(textNode);
var myUL = document.querySelector("#changeme ul");
myUL.appendChild(newLI);
</pre>
</section>
</div>
<div class="slide normal">
<header><h1>Remove Elements</h1></header>
<section class="content">
<ol>
<li>Find the node you want to remove.</li>
<li>Find the parent of that node</li>
<li>Remove the parent's child</li>
</ol>
<pre contenteditable>var lis = document.querySelectorAll('#changeme li');
lis[4].<strong>parentNode</strong>.<strong>removeChild</strong>(lis[4]);</pre>
<pre contenteditable>
var box = document.getElementById('changeme');
var list = box.getElementsByTagName('ul')[0];
var fifthitem = box.getElementsByTagName('li')[4];
var item = list.removeChild(fifthitem);
</pre>
</section>
</div>
<div class="slide normal">
<header><h1>Exercises</h1></header>
<section class="content">
<p>Slide 11 displays the text created by the markup on slide 10. Let's play with it! In the console on slide 11 do the following:</p>
<ul>
<li>Change the text of the link</li>
<li>Change the bold text</li>
<li>Change the text of one of the list items.</li>
<li>Add a list item</li>
<li>Add some bold text as a child of one of the list items</li>
<li>Remove every node you just added</li>
</ul>
</section>
</div>
<div class="slide intro">
<header><h1>Next</h1></header>
<section class="content">
<a href="part_07.html">Go</a>
</section>
</div>
</div>
</div>
<script src="slides.js">
</script>
</body> </html>