您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Web Components入门实例教程

彭世瑜 发布时间:2022-06-05 14:28:11 ,浏览量:3

代码示例



  
    .user-name {
      color: green;
    }
  
  



  // 定义组件
  class UserName extends HTMLElement {
    constructor() {
      super();

      let shadow = this.attachShadow({ mode: "closed" });

      let content = document
        .querySelector("#UserNameTemplate")
        .content.cloneNode(true);
      console.log(this.getAttribute("name"));
      content.querySelector(".user-name").innerText =
        this.getAttribute("name");

      shadow.appendChild(content);
    }
  }

  // 注册组件
  window.customElements.define("user-name", UserName);




遗留问题

  • 按照文章写的demo,this.getAttribute("name")无法获取数据,导致传参失败

参考 Web Components 入门实例教程-阮一峰

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0648s