美文网首页
JavaScript自定义html标签

JavaScript自定义html标签

作者: 杰克_王_ | 来源:发表于2021-09-01 16:47 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <!-- 创建自定义标签 -->
    <script>
        class PopUpInfo extends HTMLElement {
            get text() {
                // debugger;
                return this.getAttribute("text");
            }
            set text(val) {
                // debugger;
                this.setAttribute("text");
            }

            constructor() {
                console.log(arguments);

                super();
                console.log(this);

                // 在此定义自定义标签 我顶一个icon和text并列的
                // Create a shadow root
                let shadow = this.attachShadow({ mode: 'open' });

                // 创建我们需要的标签
                let wrapper = document.createElement("div");
                let iconBox = document.createElement("div");
                let textBox = document.createElement("div");

                // 为标签添加样式
                wrapper.setAttribute("class", "wrapper");
                iconBox.setAttribute("class", "icon");
                textBox.setAttribute("class", "text");

                // debugger;
                // 获取标签里面传递的值
                // let text = this.getAttribute("text");
                let text = this.text;
                textBox.textContent = text;

                let imgUrl;
                if (this.hasAttribute("img")) {
                    imgUrl = this.getAttribute("img");
                }
                else {
                    imgUrl = "https://note.youdao.com/web/images/dummy_user-85ee808751.jpg"; // 设置一个默认图片
                }

                var img = document.createElement("img");
                img.src = imgUrl;
                iconBox.appendChild(img);

                // 书写样式
                let style = document.createElement("style");
                let lStyleStr = `.wrapper { display: flex; justify-content: center; align-items: center; width: 100%; height: 50px;}
                .icon { margin-right: 10px; width: 50px; height: 50px;}
                .icon img { width: 100%; height: 100%;}
                .text { flex: 1; font-size: 14px; color: #333; line-height: 50px;}`;
                style.textContent = lStyleStr;

                // 将样式和dom元素挂载到页面
                shadow.appendChild(style);
                shadow.appendChild(wrapper);
                wrapper.appendChild(iconBox);
                wrapper.appendChild(textBox);
            }
        }
    </script>

    <!-- 使用自定义标签 -->
    <popup-info img="https://himg.bdimg.com/sys/portraitn/item/8f9f77616e6779696875323030390e09" text="你的文字">
    </popup-info>

    <!-- 注册自定义标签 -->
    <script>
        window.customElements.define('popup-info', PopUpInfo);
    </script>
</body>

</html>
<!-- https://www.cnblogs.com/tmbm/p/11115871.html -->

相关文章

  • JavaScript自定义html标签

  • web前端案例-电影预告动态滚动

    效果知识点:html常用标签, css浮动定位盒模型,javascript自定义动画、鼠标跟随移动特效等。 ?ht...

  • 前端小案例:电影预告动态滚动特效制作

    效果知识点:html常用标签, css浮动定位盒模型,javascript自定义动画、鼠标跟随移动特效等。?htm...

  • JavaScript学习

    标签在HTML网页中插入JavaScript代码。注意, 标签要成对出现,并把JavaScript代码在 之间。...

  • JavaScript

    如何插入JS 使用 标签在HTML网页中插入JavaScript代码。 标签要成对出现,并把JavaScript代...

  • javascript1

    1.使用 标签在html网页中插入JavaScript代码,注意 标签要成对出现,并把JavaScript代码写在...

  • 第2节.如何插入JS(脚本)

    1.使用 标签在Html网页中插入JavaScript代码。 注意 标签要成对出现,并把JavaScript代码写...

  • HTML笔记--列表标签

    HTML笔记--列表标签 标签(空格分隔): HTML 列表标签: 定义列表:带缩进的列表如这种样式的: 自定义...

  • JS入门

    JavaScript JavaScript使用   HTML 中的脚本必须位于 与 标签之间。  脚本可被...

  • JavaScript 笔记

    用法 1 外部 javascript 文件不使用 标签,直接写 javascript 代码。2 HTML 输出...

网友评论

      本文标题:JavaScript自定义html标签

      本文链接:https://www.haomeiwen.com/subject/ppzxwltx.html