-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic-listener-capture-phase-ordering.html
53 lines (48 loc) · 1.59 KB
/
dynamic-listener-capture-phase-ordering.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
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: Listener execution order when dynamically binding to the capture phase</title>
<meta name="author" title="Eugene Kashida" href="mailto:[email protected]">
</head>
<body>
<script>
class MyCustomElement extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({ mode: 'closed' });
this.addEventListener('click', event => {
console.log(5, 'host element', event.eventPhase);
});
this._shadowRoot.addEventListener('click', event => {
console.log(4, 'shadow root', event.eventPhase);
});
}
connectedCallback() {
this._shadowRoot.innerHTML = `
<button>click me</button>
`;
const button = this._shadowRoot.querySelector('button');
button.addEventListener('click', function(event) {
console.log(1, 'button', event.eventPhase);
});
}
click() {
console.log('custom click method');
const button = this._shadowRoot.querySelector('button');
button.addEventListener('click', function(event) {
console.log(2, 'button (capture)', event.eventPhase);
event.stopPropagation();
}, true);
button.addEventListener('click', function(event) {
console.log(3, 'button', event.eventPhase);
});
button.click();
}
}
customElements.define('my-custom-element', MyCustomElement);
var instance = document.createElement('my-custom-element');
document.body.appendChild(instance);
instance.click();
</script>
</body>
</html>