repl.it
@todojs/

02-light-dom-shadow-dom

HTML, CSS, JS

No description

fork
loading
Files
  • index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Light DOM</title>
</head>
<body>
<div id="test"><p>Light DOM content</p></div>
<button id="attach">AttachShadow</button>
<button id="find">document.querySelector('div > P')</button>
<pre></pre>
<script>
  const attach = document.querySelector ('#attach');
  attach.addEventListener ('click', function () {
    const div = document.querySelector ('#test');
    div.attachShadow ({mode : 'open'});
    div.shadowRoot.innerHTML = '<p>Shadow DOM content</p>';
    result.innerHTML         = '';
  });

  const result = document.querySelector ('pre');
  const find   = document.querySelector ('#find');
  find.addEventListener ('click', function () {
    const element    = document.querySelector ('div > p');
    result.innerHTML = element.innerText;
  });
</script>
</body>
</html>
result
console
?