https://jsbin.com/qogewewomi/1/edit?html,js,output
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<test-test></test-test>
<button id='out'>Outside Shadow DOM</button>
</body>
</html>
customElements.define('test-test', class extends HTMLElement {
constructor() {
super();
const node = document.createElement('template');
node.innerHTML = '<button id="in">Inside Shadow DOM</button>';
this.attachShadow({
mode: 'open'
}).appendChild(node.content);
this.shadowRoot.querySelector('#in').addEventListener('click', e => {
console.log(e.target);
setTimeout(() => {
console.log(e.target);
});
});
}
});
document.querySelector('#out').addEventListener('click', e => {
console.log(e.target);
setTimeout(() => {
console.log(e.target);
});
});
I found these inconsistent behaviors inside an event listener inside and outside of shadow DOM. When the Inside Shadow DOM
button is clicked, the console outputs:
<button id="in">Inside Shadow DOM</button>
<test-test>...</test-test>
When the Outside Shadow DOM
button is clicked, the console outputs:
<button id="out">Outside Shadow DOM</button>
<button id="out">Outside Shadow DOM</button>
Tested in Chrome, FireFox and Safari. They all have these inconsistent behaviors. I don't know if this is expected behavior or a bug?
Update: This question should not be closed. The other one doesn't answer this question.