-
Notifications
You must be signed in to change notification settings - Fork 0
/
detached-event-target.html
103 lines (93 loc) · 3.71 KB
/
detached-event-target.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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: Detached event.target</title>
<meta name="author" title="Eugene Kashida" href="mailto:[email protected]">
</head>
<body>
<script>
class Parent extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
}
connectedCallback() {
this._shadowRoot.innerHTML = `
<div id=d1>
<x-child></x-child>
</div>
`;
this._shadowRoot.addEventListener('click', event => {
console.log(`[parent#shadowRoot] event.target: ${event.target}`);
console.log(`[parent#shadowRoot] event.target.parentNode: ${event.target.parentNode}`);
// event.target => x-child
// event.target.parentNode => null
debugger;
});
this._shadowRoot.querySelector('x-child').addEventListener('click', event => {
console.log(`[parent x-child] event.target: ${event.target}`);
console.log(`[parent x-child] event.target.parentNode: ${event.target.parentNode}`);
console.log(`[parent x-child] event.target.parentNode.parentNode ${event.target.parentNode.parentNode}`);
// event.target => x-child
// event.target.parentNode => div#d1
// event.target.parentNode.parentNode => #shadowRoot
debugger;
console.log('removing x-child');
this._shadowRoot.querySelector('#d1').innerHTML = '<span>removed x-child</span>';
});
}
}
customElements.define('x-parent', Parent);
class Child extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
}
connectedCallback() {
this._shadowRoot.innerHTML = `
<div id=d2>
<button id=b1>click me</button>
</div>
`;
this._shadowRoot.addEventListener('click', event => {
console.log(`[child#shadowRoot] event.target: ${event.target}`);
console.log(`[child#shadowRoot] event.target.parentNode: ${event.target.parentNode}`);
console.log(`[child#shadowRoot] event.target.parentNode.parentNode: ${event.target.parentNode.parentNode}`);
// event.target => button#b1
// event.target.parentNode => div#d2
// event.target.parentNode.parentNode => null
debugger;
});
const d2 = this._shadowRoot.querySelector('#d2');
d2.addEventListener('click', event => {
console.log(`[child#d2] event.target: ${event.target}`);
console.log(`[child#d2] event.target.parentNode: ${event.target.parentNode}`);
console.log(`[child#d2] event.target.parentNode.parentNode: ${event.target.parentNode.parentNode}`);
// event.target => button#b1
// event.target.parentNode => div#d1
// event.target.parentNode.parentNode => #shadowRoot
debugger;
console.log('removing button');
this._shadowRoot.innerHTML = '<span>removed button</span>';
console.log(`[child#d2] event.target: ${event.target}`);
console.log(`[child#d2] event.target.parentNode: ${event.target.parentNode}`);
console.log(`[child#d2] event.target.parentNode.parentNode: ${event.target.parentNode.parentNode}`);
// event.target => button#b1
// event.target.parentNode => div#d2
// event.target.parentNode.parentNode => null
debugger;
});
}
}
customElements.define('x-child', Child);
var div = document.createElement('div');
div.innerHTML = `
<x-parent></x-parent>
`;
document.body.appendChild(div);
/*
Observations:
*/
</script>
</body>
</html>