|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Handling DOM Events</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Handling DOM Events</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>This example demonstrates how to respond to DOM events from a Node instance.</p> |
|
|
30 |
<p>Clicking one of the elements will report some event details.</p> |
|
|
31 |
</div> |
|
|
32 |
|
|
|
33 |
<div class="example"> |
|
|
34 |
<style> |
|
|
35 |
#container{ |
|
|
36 |
font-size: 200%; |
|
|
37 |
cursor: pointer; |
|
|
38 |
padding: 0 0.5em; |
|
|
39 |
margin-bottom: 0.3em; |
|
|
40 |
border-bottom: solid 1px #ccc; |
|
|
41 |
text-align: center; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
#container em{ |
|
|
45 |
color: red; |
|
|
46 |
font-weight: bold; |
|
|
47 |
font-size: 130%; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
#container strong{ |
|
|
51 |
color: green; |
|
|
52 |
font-weight: bold; |
|
|
53 |
font-family: arial black; |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
#container code{ |
|
|
57 |
background-color: #000; |
|
|
58 |
color: #CEFFA2; |
|
|
59 |
padding: 0.3em; |
|
|
60 |
font-weight: bold; |
|
|
61 |
font-family: Courier,monospace; |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
#container a{ |
|
|
65 |
color: #00f; |
|
|
66 |
padding: 0.3em; |
|
|
67 |
text-decoration: underline; |
|
|
68 |
font-family: verdana; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
.example .dd-color{ |
|
|
72 |
height: 1em; |
|
|
73 |
width: 3em; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
.example dt{ |
|
|
77 |
font-weight: normal; |
|
|
78 |
color: #999999; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
.example dd{ |
|
|
82 |
margin: 0 1.5em 0.3em; |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
.example dl{ |
|
|
86 |
margin: 0; |
|
|
87 |
} |
|
|
88 |
</style> |
|
|
89 |
|
|
|
90 |
<div id="container"> |
|
|
91 |
<p> <em>emphasis</em> <code>code</code> <strong>strong</strong> <a>anchor</a> <img src="../assets/node/images/birds.png" align="middle"/></p> |
|
|
92 |
</div> |
|
|
93 |
<div id="event-result">Click an element above to see its event data.</div> |
|
|
94 |
|
|
|
95 |
<script type="text/javascript"> |
|
|
96 |
YUI().use('node', function(Y) { |
|
|
97 |
var onClick = function(e) { |
|
|
98 |
var type = e.type, |
|
|
99 |
currentTarget = e.currentTarget, // #container |
|
|
100 |
target = e.target; // #container or a descendant |
|
|
101 |
|
|
|
102 |
Y.one('#event-result').setHTML('<dl>' + |
|
|
103 |
'<dt>Event Type: </dt>' + |
|
|
104 |
'<dd>' + e.type + '</dd>' + |
|
|
105 |
'<dt>Target Tag Name: </dt>' + |
|
|
106 |
'<dd>' + target.get('tagName') + '</dd>' + |
|
|
107 |
'<dt>Color of Target\'s Font: </dt>' + |
|
|
108 |
'<div class="dd-color" style="background-color:' + target.getComputedStyle('color') + ';">' + '</div>' + |
|
|
109 |
'<dt>CurrentTarget Tag Name & Id: </dt>' + |
|
|
110 |
'<dd>' + currentTarget.get('tagName') + '#' + currentTarget.get('id') + '</dd>' + |
|
|
111 |
'</dl>'); |
|
|
112 |
}; |
|
|
113 |
|
|
|
114 |
Y.one('#container').on('click', onClick); |
|
|
115 |
Y.one('#container').on('dblclick', onClick); |
|
|
116 |
}); |
|
|
117 |
</script> |
|
|
118 |
|
|
|
119 |
</div> |
|
|
120 |
|
|
|
121 |
<h2>Setting up the HTML</h2> |
|
|
122 |
<p>First we need some HTML to work with.</p> |
|
|
123 |
<pre class="code prettyprint"><div id="container"> |
|
|
124 |
<p> |
|
|
125 |
<em>emphasis</em> <code>code</code> <strong>strong</strong> <a>anchor</a> |
|
|
126 |
<img src="../assets/node/images/birds.png" align="middle"/> |
|
|
127 |
</p> |
|
|
128 |
</div> |
|
|
129 |
<div id="event-result">Click an element above to see its event data.</div></pre> |
|
|
130 |
|
|
|
131 |
<h2>Creating the Event Handler</h2> |
|
|
132 |
<p>Next we'll create a handler to run when the event is fired. In our handler |
|
|
133 |
we'll update the <code>#event-result</code> node with some data available through the event.</p> |
|
|
134 |
<pre class="code prettyprint">var onClick = function(e) { |
|
|
135 |
var type = e.type, |
|
|
136 |
currentTarget = e.currentTarget, // #container |
|
|
137 |
target = e.target; // #container or a descendant |
|
|
138 |
|
|
|
139 |
Y.one('#event-result').setHTML('<dl>' + |
|
|
140 |
'<dt>Event Type: </dt>' + |
|
|
141 |
'<dd>' + e.type + '</dd>' + |
|
|
142 |
'<dt>Target Tag Name: </dt>' + |
|
|
143 |
'<dd>' + target.get('tagName') + '</dd>' + |
|
|
144 |
'<dt>Color of Target's Font: </dt>' + |
|
|
145 |
'<dd class="dd-color" style="background-color:' + target.getStyle('color') + ';">' + '</dd>' + |
|
|
146 |
'<dt>CurrentTarget Tag Name & Id: </dt>' + |
|
|
147 |
'<dd>' + currentTarget.get('tagName') + '#' + currentTarget.get('id') + '</dd>' + |
|
|
148 |
'</dl>'); |
|
|
149 |
};</pre> |
|
|
150 |
|
|
|
151 |
|
|
|
152 |
<h2>Listening for Events</h2> |
|
|
153 |
<p>We can assign our handler to the container of the objects by using the <code>Y.one</code>. |
|
|
154 |
Clicking on any object in the container will bubble the event to the container. |
|
|
155 |
We're using the 'on' method to subscribe to the click and dblclick events.</p> |
|
|
156 |
<pre class="code prettyprint">Y.one('#container').on('click', onClick); |
|
|
157 |
Y.one('#container').on('dblclick', onClick);</pre> |
|
|
158 |
|
|
|
159 |
|
|
|
160 |
<h2>Complete Example Source</h2> |
|
|
161 |
<pre class="code prettyprint"><style> |
|
|
162 |
#container{ |
|
|
163 |
font-size: 200%; |
|
|
164 |
cursor: pointer; |
|
|
165 |
padding: 0 0.5em; |
|
|
166 |
margin-bottom: 0.3em; |
|
|
167 |
border-bottom: solid 1px #ccc; |
|
|
168 |
text-align: center; |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
#container em{ |
|
|
172 |
color: red; |
|
|
173 |
font-weight: bold; |
|
|
174 |
font-size: 130%; |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
#container strong{ |
|
|
178 |
color: green; |
|
|
179 |
font-weight: bold; |
|
|
180 |
font-family: arial black; |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
#container code{ |
|
|
184 |
background-color: #000; |
|
|
185 |
color: #CEFFA2; |
|
|
186 |
padding: 0.3em; |
|
|
187 |
font-weight: bold; |
|
|
188 |
font-family: Courier,monospace; |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
#container a{ |
|
|
192 |
color: #00f; |
|
|
193 |
padding: 0.3em; |
|
|
194 |
text-decoration: underline; |
|
|
195 |
font-family: verdana; |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
.example .dd-color{ |
|
|
199 |
height: 1em; |
|
|
200 |
width: 3em; |
|
|
201 |
} |
|
|
202 |
|
|
|
203 |
.example dt{ |
|
|
204 |
font-weight: normal; |
|
|
205 |
color: #999999; |
|
|
206 |
} |
|
|
207 |
|
|
|
208 |
.example dd{ |
|
|
209 |
margin: 0 1.5em 0.3em; |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
.example dl{ |
|
|
213 |
margin: 0; |
|
|
214 |
} |
|
|
215 |
</style> |
|
|
216 |
|
|
|
217 |
<div id="container"> |
|
|
218 |
<p> <em>emphasis</em> <code>code</code> <strong>strong</strong> <a>anchor</a> <img src="../assets/node/images/birds.png" align="middle"/></p> |
|
|
219 |
</div> |
|
|
220 |
<div id="event-result">Click an element above to see its event data.</div> |
|
|
221 |
|
|
|
222 |
<script type="text/javascript"> |
|
|
223 |
YUI().use('node', function(Y) { |
|
|
224 |
var onClick = function(e) { |
|
|
225 |
var type = e.type, |
|
|
226 |
currentTarget = e.currentTarget, // #container |
|
|
227 |
target = e.target; // #container or a descendant |
|
|
228 |
|
|
|
229 |
Y.one('#event-result').setHTML('<dl>' + |
|
|
230 |
'<dt>Event Type: </dt>' + |
|
|
231 |
'<dd>' + e.type + '</dd>' + |
|
|
232 |
'<dt>Target Tag Name: </dt>' + |
|
|
233 |
'<dd>' + target.get('tagName') + '</dd>' + |
|
|
234 |
'<dt>Color of Target\'s Font: </dt>' + |
|
|
235 |
'<div class="dd-color" style="background-color:' + target.getComputedStyle('color') + ';">' + '</div>' + |
|
|
236 |
'<dt>CurrentTarget Tag Name & Id: </dt>' + |
|
|
237 |
'<dd>' + currentTarget.get('tagName') + '#' + currentTarget.get('id') + '</dd>' + |
|
|
238 |
'</dl>'); |
|
|
239 |
}; |
|
|
240 |
|
|
|
241 |
Y.one('#container').on('click', onClick); |
|
|
242 |
Y.one('#container').on('dblclick', onClick); |
|
|
243 |
}); |
|
|
244 |
</script></pre> |
|
|
245 |
|
|
|
246 |
</div> |
|
|
247 |
</div> |
|
|
248 |
</div> |
|
|
249 |
|
|
|
250 |
<div class="yui3-u-1-4"> |
|
|
251 |
<div class="sidebar"> |
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
<div class="sidebox"> |
|
|
256 |
<div class="hd"> |
|
|
257 |
<h2 class="no-toc">Examples</h2> |
|
|
258 |
</div> |
|
|
259 |
|
|
|
260 |
<div class="bd"> |
|
|
261 |
<ul class="examples"> |
|
|
262 |
|
|
|
263 |
|
|
|
264 |
<li data-description="Using selectors and property accessors with Node."> |
|
|
265 |
<a href="properties.html">Set and Get Properties</a> |
|
|
266 |
</li> |
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
<li data-description="Using DOM methods with Node."> |
|
|
271 |
<a href="dom-node.html">DOM Methods</a> |
|
|
272 |
</li> |
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
<li data-description="Building a simple store and shopping cart."> |
|
|
277 |
<a href="store.html">DOM Methods - Store</a> |
|
|
278 |
</li> |
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
<li data-description="Listening for DOM events with Node instances."> |
|
|
283 |
<a href="events.html">Handling DOM Events</a> |
|
|
284 |
</li> |
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
<li data-description="NodeList provides Node functionality for manipulating multiple nodes at once."> |
|
|
289 |
<a href="nodelist.html">Using NodeList - Simple</a> |
|
|
290 |
</li> |
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
<li data-description="How to use multiple NodeList features to build a simple game."> |
|
|
295 |
<a href="ducks.html">Using NodeList - Ducks Game</a> |
|
|
296 |
</li> |
|
|
297 |
|
|
|
298 |
|
|
|
299 |
|
|
|
300 |
<li data-description="Using a single event listener to handle events on multiple nodes."> |
|
|
301 |
<a href="node-evt-delegation.html">Delegating Node Events</a> |
|
|
302 |
</li> |
|
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
|
306 |
<li data-description="This example demonstrates how to position an element in page coordinates."> |
|
|
307 |
<a href="node-xy.html">Node Positioning</a> |
|
|
308 |
</li> |
|
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
312 |
<li data-description="This example demonstrates how to set styles and get style information."> |
|
|
313 |
<a href="node-style.html">Node Styling</a> |
|
|
314 |
</li> |
|
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
<li data-description="This example demonstrates how to insert content into a Node."> |
|
|
319 |
<a href="node-insert.html">Adding Node Content - Burger Builder</a> |
|
|
320 |
</li> |
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
<li data-description="This example demonstrates how to show and hide a Node."> |
|
|
325 |
<a href="node-view.html">Showing and Hiding</a> |
|
|
326 |
</li> |
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
|
|
|
335 |
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
</ul> |
|
|
344 |
</div> |
|
|
345 |
</div> |
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
<div class="sidebox"> |
|
|
350 |
<div class="hd"> |
|
|
351 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
352 |
</div> |
|
|
353 |
|
|
|
354 |
<div class="bd"> |
|
|
355 |
<ul class="examples"> |
|
|
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 |
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States."> |
|
|
381 |
<a href="../node-focusmanager/node-focusmanager-toolbar.html">Accessible Toolbar</a> |
|
|
382 |
</li> |
|
|
383 |
|
|
|
384 |
|
|
|
385 |
|
|
|
386 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
387 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
388 |
</li> |
|
|
389 |
|
|
|
390 |
|
|
|
391 |
|
|
|
392 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
393 |
<a href="../event/basic-example.html">Simple DOM Events</a> |
|
|
394 |
</li> |
|
|
395 |
|
|
|
396 |
|
|
|
397 |
|
|
|
398 |
<li data-description="Example Photo Browser application."> |
|
|
399 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
400 |
</li> |
|
|
401 |
|
|
|
402 |
|
|
|
403 |
|
|
|
404 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
405 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
406 |
</li> |
|
|
407 |
|
|
|
408 |
|
|
|
409 |
|
|
|
410 |
<li data-description="Use IO to request XML data from a remote web service."> |
|
|
411 |
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a> |
|
|
412 |
</li> |
|
|
413 |
|
|
|
414 |
|
|
|
415 |
|
|
|
416 |
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources."> |
|
|
417 |
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a> |
|
|
418 |
</li> |
|
|
419 |
|
|
|
420 |
|
|
|
421 |
</ul> |
|
|
422 |
</div> |
|
|
423 |
</div> |
|
|
424 |
|
|
|
425 |
</div> |
|
|
426 |
</div> |
|
|
427 |
</div> |
|
|
428 |
</div> |
|
|
429 |
|
|
|
430 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
431 |
<script>prettyPrint();</script> |
|
|
432 |
|
|
|
433 |
<script> |
|
|
434 |
YUI.Env.Tests = { |
|
|
435 |
examples: [], |
|
|
436 |
project: '../assets', |
|
|
437 |
assets: '../assets/node', |
|
|
438 |
name: 'events', |
|
|
439 |
title: 'Handling DOM Events', |
|
|
440 |
newWindow: '', |
|
|
441 |
auto: false |
|
|
442 |
}; |
|
|
443 |
YUI.Env.Tests.examples.push('properties'); |
|
|
444 |
YUI.Env.Tests.examples.push('dom-node'); |
|
|
445 |
YUI.Env.Tests.examples.push('store'); |
|
|
446 |
YUI.Env.Tests.examples.push('events'); |
|
|
447 |
YUI.Env.Tests.examples.push('nodelist'); |
|
|
448 |
YUI.Env.Tests.examples.push('ducks'); |
|
|
449 |
YUI.Env.Tests.examples.push('node-evt-delegation'); |
|
|
450 |
YUI.Env.Tests.examples.push('node-xy'); |
|
|
451 |
YUI.Env.Tests.examples.push('node-style'); |
|
|
452 |
YUI.Env.Tests.examples.push('node-insert'); |
|
|
453 |
YUI.Env.Tests.examples.push('node-view'); |
|
|
454 |
YUI.Env.Tests.examples.push('node-focusmanager-toolbar'); |
|
|
455 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
456 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
457 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
458 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
459 |
YUI.Env.Tests.examples.push('weather'); |
|
|
460 |
YUI.Env.Tests.examples.push('xdr'); |
|
|
461 |
|
|
|
462 |
</script> |
|
|
463 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
464 |
|
|
|
465 |
|
|
|
466 |
|
|
|
467 |
</body> |
|
|
468 |
</html> |