https://developer.mozilla.org/zh-CN/docs/Web/API/Element/attachShadow
    image.png

    1. <body>
    2. <p>全局的P标签</p>
    3. <div id="box"></div>
    4. <script>
    5. // closed外界无法访问 shadowDOM
    6. const $el = document
    7. .getElementById('box')
    8. .attachShadow({ mode: 'closed' })
    9. const elem = document.createElement('p')
    10. elem.innerHTML = 'shadowDOM is children element'
    11. const elemStyle = document.createElement('style')
    12. elemStyle.textContent = `p {color: red;font-size: 32px}`
    13. // shadowDOM 里面添加 style 标签和 p标签
    14. $el.appendChild(elemStyle)
    15. $el.appendChild(elem)
    16. </script>
    17. </body>