demo.html 961 B

123456789101112131415161718192021222324252627282930
  1. {{ $uniq := .Inner | htmlEscape | base64Encode | truncate 15 "" }}
  2. <div class="demo-container">
  3. <div id="demo-{{ $uniq }}">
  4. </div>
  5. <template id="template-{{ $uniq }}">
  6. <p>You must be joking.</p>
  7. <style>
  8. p {
  9. color: red !important;
  10. font-family: cursive;
  11. }
  12. </style>
  13. <script>
  14. demo.querySelector('p').addEventListener('click', function() {
  15. alert('hello');
  16. })
  17. </script>
  18. </template>
  19. <script>
  20. (function() {
  21. var root = document.getElementById('demo-{{ $uniq }}');
  22. root.attachShadow({mode: 'open'});
  23. var template = document.getElementById('template-{{ $uniq }}');
  24. var script = template.content.querySelector('script');
  25. script.textContent = 'var demo = document.getElementById(\'demo-{{ $uniq }}\').shadowRoot;' + script.textContent;
  26. root.shadowRoot.appendChild(document.importNode(template.content, true));
  27. console.log(root);
  28. })();
  29. </script>
  30. </div>