|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>Simple DOM Events</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<!--begin custom header content for this example--> |
|
|
25 |
<style type="text/css"> |
|
|
26 |
#container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;} |
|
|
27 |
</style> |
|
|
28 |
<!--end custom header content for this example--> |
|
|
29 |
|
|
|
30 |
</head> |
|
|
31 |
|
|
|
32 |
<body class=" yui-skin-sam"> |
|
|
33 |
|
|
|
34 |
<h1>Simple DOM Events</h1> |
|
|
35 |
|
|
|
36 |
<div class="exampleIntro"> |
|
|
37 |
<p>Clicking in the blue box will pop up a "Hello World!" alert window. Clicking on the first link will take you to the YUI website; clicking on the second link, which has the same <code>href</code> attribute, will pop up an alert instead and not navigate to a new page.</p> |
|
|
38 |
|
|
|
39 |
<p>Event Utility is used here to do two things:</p> |
|
|
40 |
|
|
|
41 |
<ol> |
|
|
42 |
<li>Attach the <code>click</code> event handler to the blue box;</li> |
|
|
43 |
<li>Attach an event handler to the second <code><a></code> element that uses <code>preventDefault()</code> to prevent the link, when clicked, from navigating to a new page. </li> |
|
|
44 |
|
|
|
45 |
</ol> |
|
|
46 |
</div> |
|
|
47 |
|
|
|
48 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
<script language="javascript"> |
|
|
53 |
|
|
|
54 |
YUI({base:"../../build/", timeout: 10000}).use("node", |
|
|
55 |
function(Y) { |
|
|
56 |
|
|
|
57 |
//A function that pops up a "Hello World" alert: |
|
|
58 |
var helloWorld = function(e) { |
|
|
59 |
Y.log("helloWorld function firing.", "info", "example"); |
|
|
60 |
alert("Hello World!"); |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
//subscribe the helloWorld function as an event |
|
|
64 |
//handler for the click event on the container |
|
|
65 |
//div: |
|
|
66 |
Y.on("click", helloWorld, "#container"); |
|
|
67 |
|
|
|
68 |
//A function that pops up an alert and |
|
|
69 |
//prevents the default behavior of the event |
|
|
70 |
//for which it is a handler: |
|
|
71 |
var interceptLink = function(e) { |
|
|
72 |
e.preventDefault(); |
|
|
73 |
Y.log("You clicked on the second YUI link.", "info", "example"); |
|
|
74 |
alert("You clicked on the second YUI link. Because *preventDefault* was called, this link will not navigate away from the page."); |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
//subscribe our interceptLink function |
|
|
78 |
//as a click handler for the second anchor |
|
|
79 |
//element: |
|
|
80 |
Y.on("click", interceptLink, "#secondA"); |
|
|
81 |
|
|
|
82 |
//log message to indicate that the example is ready: |
|
|
83 |
Y.log("When you begin interacting with the example at left, you'll see log messages appear here.", "info", "example"); |
|
|
84 |
}); |
|
|
85 |
|
|
|
86 |
</script> |
|
|
87 |
|
|
|
88 |
<div id="container"> |
|
|
89 |
<p>Click for Hello World alert.</p> |
|
|
90 |
</div> |
|
|
91 |
<p><a href="http://developer.yahoo.com/yui" id="firstA">The YUI Library. (Link navigates away from page.)</a></p> |
|
|
92 |
|
|
|
93 |
<p><a href="http://developer.yahoo.com/yui" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p> |
|
|
94 |
|
|
|
95 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
96 |
|
|
|
97 |
</body> |
|
|
98 |
</html> |