|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Simple 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: Simple DOM Events</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><style scoped> |
|
|
29 |
#container { |
|
|
30 |
background-color: #F5E3B1; |
|
|
31 |
border: 1px solid #C2AE6D; |
|
|
32 |
height: 76px; |
|
|
33 |
/*padding: 1em 0 0 1em;*/ |
|
|
34 |
padding-left: 1em; |
|
|
35 |
cursor: pointer; |
|
|
36 |
} |
|
|
37 |
#demo .hello { |
|
|
38 |
background: url("../assets/event/earth.png") no-repeat scroll 141px -62px #000000; |
|
|
39 |
color: #C3A7CD; |
|
|
40 |
border-color: #000; |
|
|
41 |
font-size: 150%; |
|
|
42 |
} |
|
|
43 |
#example-canvas #demo a { |
|
|
44 |
color: #00c; |
|
|
45 |
text-decoration: underline; |
|
|
46 |
} |
|
|
47 |
.message { |
|
|
48 |
visibility: hidden; |
|
|
49 |
} |
|
|
50 |
</style> |
|
|
51 |
|
|
|
52 |
<div class="intro"> |
|
|
53 |
<p>Clicking in the yellow box will give hello world feedback. |
|
|
54 |
Clicking on the first link will take you to the YUI website; clicking on the |
|
|
55 |
second link, which has the same <code>href</code> attribute, will display |
|
|
56 |
a message instead, and not navigate to a new page.</p> |
|
|
57 |
|
|
|
58 |
<p>Event Utility is used here to do two things:</p> |
|
|
59 |
|
|
|
60 |
<ol> |
|
|
61 |
<li>Attach the <code>click</code> event handler to the yellow box;</li> |
|
|
62 |
<li>Attach an event handler to the second <code><a></code> |
|
|
63 |
element that uses <code>preventDefault()</code> to prevent the link, |
|
|
64 |
when clicked, from navigating to a new page. </li> |
|
|
65 |
|
|
|
66 |
</ol> |
|
|
67 |
</div> |
|
|
68 |
|
|
|
69 |
<div class="example yui3-skin-sam"> |
|
|
70 |
<div id="demo"> |
|
|
71 |
<div id="container"> |
|
|
72 |
<p>Click for Hello World test.</p> |
|
|
73 |
</div> |
|
|
74 |
<p><a href="http://yuilibrary.com" id="firstA">The YUI Library. (Link navigates away from page.)</a></p> |
|
|
75 |
|
|
|
76 |
<a href="http://yuilibrary.com" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a> |
|
|
77 |
<div class="message"> |
|
|
78 |
When you clicked on the second link, *preventDefault* was called, so it did not navigate away from the page. |
|
|
79 |
</div> |
|
|
80 |
|
|
|
81 |
</div> |
|
|
82 |
|
|
|
83 |
<script> |
|
|
84 |
YUI().use('node', function (Y) { |
|
|
85 |
// A function that gives hello world feedback: |
|
|
86 |
var helloWorld = function(e) { |
|
|
87 |
e.target.setHTML("<p>Hello World!</p>"); |
|
|
88 |
Y.one('#container').addClass('hello'); |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
// subscribe the helloWorld function as an event handler for the click |
|
|
92 |
// event on the container div: |
|
|
93 |
var node = Y.one("#container"); |
|
|
94 |
|
|
|
95 |
node.on("click", helloWorld); |
|
|
96 |
|
|
|
97 |
// A function that displays a message and prevents the default behavior of |
|
|
98 |
// the event for which it is a handler: |
|
|
99 |
var interceptLink = function(e) { |
|
|
100 |
e.preventDefault(); |
|
|
101 |
Y.one('.message').setStyle('visibility', 'visible'); |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
// subscribe our interceptLink function as a click handler for the second |
|
|
105 |
// anchor element: |
|
|
106 |
Y.one('#secondA').on("click", interceptLink); |
|
|
107 |
}); |
|
|
108 |
</script> |
|
|
109 |
|
|
|
110 |
|
|
|
111 |
</div> |
|
|
112 |
|
|
|
113 |
<h2>Reacting to the <code>click</code> event</h2> |
|
|
114 |
|
|
|
115 |
<p>To illustrate event handling syntax, we'll create a <code><div></code> and |
|
|
116 |
give some feedback when that <code><div></code> is clicked.</p> |
|
|
117 |
|
|
|
118 |
<pre class="code prettyprint">// Create a YUI instance and load the 'node' module |
|
|
119 |
YUI().use('node', function (Y) { |
|
|
120 |
|
|
|
121 |
// A function that gives hello world feedback: |
|
|
122 |
var helloWorld = function(e) { |
|
|
123 |
e.target.setHTML("<p>Hello World!</p>"); |
|
|
124 |
Y.one('#container').addClass('hello'); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
...</pre> |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
<p>We now use the Node's <code>on</code> method to attach our |
|
|
131 |
<code>helloWorld</code> function as a handler for the <code>click</code> event. |
|
|
132 |
|
|
|
133 |
<pre class="code prettyprint">// Point to the container div with the CSS selector |
|
|
134 |
var node = Y.one('#container'); |
|
|
135 |
|
|
|
136 |
node.on("click", helloWorld);</pre> |
|
|
137 |
|
|
|
138 |
|
|
|
139 |
<p>Almost all event handling begins with a premise just this simple: we have an |
|
|
140 |
element to which something might happen (eg, it might be clicked) and, when |
|
|
141 |
that <em>does</em> happen, we want to do something (eg, |
|
|
142 |
<code>helloWorld</code>).</p> |
|
|
143 |
|
|
|
144 |
<h2 id="prevent">Preventing event behavior</h2> |
|
|
145 |
|
|
|
146 |
<p>In some cases, you may want to replace the default behavior of an event. |
|
|
147 |
For example, let's say you have two links on the page:</p> |
|
|
148 |
|
|
|
149 |
<pre class="code prettyprint"><p><a href="http://yuilibrary.com/" id="firstA">The YUI Library. (Link navigates away from page.)</a></p> |
|
|
150 |
|
|
|
151 |
<p><a href="http://yuilibrary.com/" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p></pre> |
|
|
152 |
|
|
|
153 |
|
|
|
154 |
<p>Let's say that when the second link is clicked, instead of navigating away |
|
|
155 |
from the current page, you want to display a message. |
|
|
156 |
The event object passed to your event handler is a facade — not |
|
|
157 |
the actual browser event object. |
|
|
158 |
This facade supports the <code>preventDefault</code> method for cancelling |
|
|
159 |
inherent browser behavior such as anchor links loading a new page.</p> |
|
|
160 |
|
|
|
161 |
<pre class="code prettyprint">// A function that stops the browser from navigating away |
|
|
162 |
// from the page, and instead displays a message. |
|
|
163 |
var interceptLink = function(e) { |
|
|
164 |
e.preventDefault(); |
|
|
165 |
Y.one('.message').setStyle('visibility', 'visible'); |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
// subscribe our interceptLink function to the second anchor |
|
|
169 |
Y.one('#secondA').on("click", interceptLink);</pre> |
|
|
170 |
|
|
|
171 |
|
|
|
172 |
<h2 id="fullcode">Full Code Listing</h2> |
|
|
173 |
<pre class="code prettyprint"><div id="demo"> |
|
|
174 |
<div id="container"> |
|
|
175 |
<p>Click for Hello World test.</p> |
|
|
176 |
</div> |
|
|
177 |
<p><a href="http://yuilibrary.com" id="firstA">The YUI Library. (Link navigates away from page.)</a></p> |
|
|
178 |
|
|
|
179 |
<a href="http://yuilibrary.com" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a> |
|
|
180 |
<div class="message"> |
|
|
181 |
When you clicked on the second link, *preventDefault* was called, so it did not navigate away from the page. |
|
|
182 |
</div> |
|
|
183 |
|
|
|
184 |
</div> |
|
|
185 |
|
|
|
186 |
<script> |
|
|
187 |
YUI().use('node', function (Y) { |
|
|
188 |
// A function that gives hello world feedback: |
|
|
189 |
var helloWorld = function(e) { |
|
|
190 |
e.target.setHTML("<p>Hello World!</p>"); |
|
|
191 |
Y.one('#container').addClass('hello'); |
|
|
192 |
} |
|
|
193 |
|
|
|
194 |
// subscribe the helloWorld function as an event handler for the click |
|
|
195 |
// event on the container div: |
|
|
196 |
var node = Y.one("#container"); |
|
|
197 |
|
|
|
198 |
node.on("click", helloWorld); |
|
|
199 |
|
|
|
200 |
// A function that displays a message and prevents the default behavior of |
|
|
201 |
// the event for which it is a handler: |
|
|
202 |
var interceptLink = function(e) { |
|
|
203 |
e.preventDefault(); |
|
|
204 |
Y.one('.message').setStyle('visibility', 'visible'); |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
// subscribe our interceptLink function as a click handler for the second |
|
|
208 |
// anchor element: |
|
|
209 |
Y.one('#secondA').on("click", interceptLink); |
|
|
210 |
}); |
|
|
211 |
</script></pre> |
|
|
212 |
|
|
|
213 |
|
|
|
214 |
</div> |
|
|
215 |
</div> |
|
|
216 |
</div> |
|
|
217 |
|
|
|
218 |
<div class="yui3-u-1-4"> |
|
|
219 |
<div class="sidebar"> |
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
<div class="sidebox"> |
|
|
224 |
<div class="hd"> |
|
|
225 |
<h2 class="no-toc">Examples</h2> |
|
|
226 |
</div> |
|
|
227 |
|
|
|
228 |
<div class="bd"> |
|
|
229 |
<ul class="examples"> |
|
|
230 |
|
|
|
231 |
|
|
|
232 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
233 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
234 |
</li> |
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
|
238 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
239 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
240 |
</li> |
|
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
245 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
246 |
</li> |
|
|
247 |
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
</ul> |
|
|
260 |
</div> |
|
|
261 |
</div> |
|
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
<div class="sidebox"> |
|
|
266 |
<div class="hd"> |
|
|
267 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
268 |
</div> |
|
|
269 |
|
|
|
270 |
<div class="bd"> |
|
|
271 |
<ul class="examples"> |
|
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
<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."> |
|
|
281 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
282 |
</li> |
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
287 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
288 |
</li> |
|
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
<li data-description="Example Photo Browser application."> |
|
|
293 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
294 |
</li> |
|
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
299 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
300 |
</li> |
|
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
|
304 |
<li data-description="Use IO to request data over HTTP."> |
|
|
305 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
306 |
</li> |
|
|
307 |
|
|
|
308 |
|
|
|
309 |
</ul> |
|
|
310 |
</div> |
|
|
311 |
</div> |
|
|
312 |
|
|
|
313 |
</div> |
|
|
314 |
</div> |
|
|
315 |
</div> |
|
|
316 |
</div> |
|
|
317 |
|
|
|
318 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
319 |
<script>prettyPrint();</script> |
|
|
320 |
|
|
|
321 |
<script> |
|
|
322 |
YUI.Env.Tests = { |
|
|
323 |
examples: [], |
|
|
324 |
project: '../assets', |
|
|
325 |
assets: '../assets/event', |
|
|
326 |
name: 'basic-example', |
|
|
327 |
title: 'Simple DOM Events', |
|
|
328 |
newWindow: '', |
|
|
329 |
auto: false |
|
|
330 |
}; |
|
|
331 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
332 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
333 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
334 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
335 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
336 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
337 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
338 |
YUI.Env.Tests.examples.push('get'); |
|
|
339 |
|
|
|
340 |
</script> |
|
|
341 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
</body> |
|
|
346 |
</html> |