/
delegate.html
124 lines (92 loc) · 2.2 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
<!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 cuba.js</title>
<style type="text/css">
.hidden ul{display: none;margin-top: 10px;margin-left: 30px;list-style:square}
code {
font-size: 1.2em;
}
pre,code {
font-family: menlo,'dejavu sans mono',monospace;
}
pre{
padding: 5px 10px;
margin-bottom: 20px;
overflow: auto;
font-family: courier;
background: ghostwhite;
border: 1px solid #ddd;
}
code {
padding: 2px 5px;
}
</style>
<script type="text/javascript" src="src/cuba.js"></script>
<script type="text/javascript">
function init() {
cuba.delegate(document.getElementById('menu'), 'li a', 'click', function( e ){
var ul = e.target.parentNode.querySelector("ul")
if(ul.style.display == "none") {
ul.style.display = "block"
} else {
ul.style.display = "none"
}
console.log(e.target)
}, false);
}
cuba.ready(function(){
init()
})
</script>
</head>
<body>
<h1>Cuba.js Event Delegation</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>
cuba.ready(function(){
cuba.delegate($('#menu'), 'li a', 'click', function( e ){
var ul = e.target.parentNode.querySelector("ul")
if(ul.style.display == "none") {
ul.style.display = "block"
} else {
ul.style.display = "none"
}
console.log(e.target)
}, false);
})
</code></pre>
</body>
</html>