/
delegate.html
147 lines (110 loc) · 2.54 KB
/
delegate.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Event Delegation on CUI2 AMD Style</title>
<style type="text/css">
pre, code {
padding: 5px 10px;
margin-bottom: 20px;
overflow: auto;
font-family: courier;
background: ghostwhite;
border: 1px solid #ddd;
}
code {
padding: 2px 5px;
}
pre code {
padding: 0;
margin-bottom: 0;
border: 0;
}
pre:before,
pre:after {
content: '';
display: none;
width: 100%;
position: absolute;
left: 0;
}
pre:before {
border-top: 1px solid hsla( 0, 0%, 100%, 1 );
top: 0;
}
pre:after {
border-top: 1px solid hsla( 0, 0%, 0%, 0.05 );
bottom: 0px;
}
</style>
<script type="text/javascript" src="amd.js"></script>
<script type="text/javascript">
require(["ready","dom","delegate"], function(r, dom, d){
var ready = r.ready,
delegate = d.delegate;
ready(function(){
delegate(dom.select("#menu"), 'li a', 'click', function( e ){
var ul = e.target.parentNode.querySelector("ul")
if(ul.style.display == "none") {
ul.css("display: block")
} else {
ul.css("display: none")
}
if(window.console) console.log(e.target)
}, false);
})
});
</script>
</head>
<body>
<h1>Event Delegation on CUI2 AMD Style</h1>
<ul id="menu">
<li>
<a href="#">Event Delegation List 1</a>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
</ul>
</li>
<li>
<a href="#">Event Delegation List 2</a>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
</ul>
</li>
<li>
<a href="#">Event Delegation List 3</a>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
</ul>
</li>
</ul>
<pre><code>
require(["ready","dom","delegate"], function(r, dom, d){
var ready = r.ready,
delegate = d.delegate;
ready(function(){
delegate(dom.select("#menu"), 'li a', 'click', function( e ){
var ul = e.target.parentNode.querySelector("ul")
if(ul.style.display == "none") {
ul.css("display: block")
} else {
ul.css("display: none")
}
if(window.console) console.log(e.target)
}, false);
})
</code></pre>
</body>
</html>