自定义元素的创建及css选中

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        /*所有自定义元素*/
        :defined{
            color: red;
        }
        /*指定的自定义元素*/
        simple-custom:defined{
           font-size: 24px;
        }
    </style>
</head>

<body>
<simple-custom text="我是自定义文本"></simple-custom>
</body>
<script>
    customElements.define('simple-custom',
        class extends HTMLElement {
            constructor() {
                super();
                let divElem = document.createElement('div');
                divElem.textContent = this.getAttribute('text');

                let shadowRoot = this.attachShadow({mode: 'open'})
                    .appendChild(divElem);
            }
        })
</script>
</html>

猜你喜欢

转载自blog.csdn.net/github_38108899/article/details/86689907
今日推荐