@todojs/

01.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
30
31
32
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>attachShadow</title>
</head>
<body>

<p>Test</p>
<button id="find">document.querySelectorAll('p')</button>
<button id="attach">AttachShadow</button>
<div id="test"></div>
<pre></pre>
<script>
  const attach = document.querySelector ('#attach');
  const div    = document.querySelector ('#test');
  const find   = document.querySelector ('#find');
  const result = document.querySelector ('pre');

  attach.addEventListener ('click', function () {
    div.attachShadow ({mode : 'open'});
    div.shadowRoot.innerHTML = '<p>DIV shadow content (&lt;p&gt;)</p>';
    result.innerHTML         = '';
  });

  find.addEventListener ('click', function () {
    const element    = document.querySelectorAll ('p');
    result.innerHTML = 'found ' + element.length + ' elements';
  });
</script>
</body>
</html>
result
console