-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.html
102 lines (80 loc) · 2.35 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>CSS Selector Listeners</title>
<script type="text/javascript" src="selector-listeners.js"></script>
<style>
body {
padding: 1em;
font-family: Arial;
}
.fields {
display: flex;
max-width: 500px;
margin: 0 0 1em;
}
.fields button {
white-space: nowrap;
}
#selectorText,
#tagText {
flex-grow: 1;
margin-right: 0.5em;
}
#container0,
#container1 {
min-height: 100px;
margin: 1em 0;
background: #eee;
border: 1px solid #ccc;
}
#container0 > *, #container1 > * {
margin: 10px;
background: #ADDFFD;
}
#console_warning {
text-align: center;
color: #BF6F6A;
}
</style>
</head>
<body>
<section class="fields">
<input id="selectorText" type="text" placeholder="Enter at-rules (optional) and a selector (separate with &&)" />
<button id="addListener">Add Listener</button>
</section>
<section class="fields">
<input id="tagText" type="text" placeholder="Enter tag name of an element to add" />
<button id="addElement">Add Element</button>
</section>
<section class="container" id="container0"></section>
<section class="container" id="container1"></section>
<div id="console_warning">TURN ON YOUR CONSOLE!</div>
</body>
<script type="text/javascript">
(function(){
var printListener = function(event){
console.log('------ Matched! ------');
console.log('selector: ' + event.selector);
console.log('event:', event);
}
document.getElementById('addListener').addEventListener('click', function(){
var selector = document.getElementById('selectorText').value;
if (selector) {
document.addSelectorListener(selector.split('&&'), printListener);
}
});
document.getElementById('addElement').addEventListener('click', function(){
var tag = document.getElementById('tagText').value || 'div';
var div = document.createElement(tag);
div.innerHTML = tag.toUpperCase() + " element";
document.getElementById('container' + Math.floor(Math.random() * 2)).appendChild(div);
});
document.addEventListener('click', function(e){
var parent = e.target.parentNode;
if (parent && parent.className == 'container') parent.removeChild(e.target);
});
})();
</script>
</html>