首页 > Web开发 > 详细

[HTML5] Build Flexible HTML with HTMLTemplates using Slots and Web Components

时间:2019-02-10 21:31:03      阅读:181      评论:0      收藏:0      [点我收藏+]

HTMLTemplates are part of the web components specification. In this lesson we will learn what are HTML templates, how to use them and how to use the powerful HTMLTemplates slots inside a web component.

 

<script>
    const templateString = `
    <div>Some text</div>
    <form>
        <input name="test"/>
    </form>
    <slot name="slot1"></slot>
    `;
    const template = document.createElement(template);
    template.innerHTML = templateString;
    class MyWebComponent extends HTMLElement {
        constructor() {
            super();
        }

        connectedCallback() {
            this.appendChild(template.content.cloneNode(true));
        }
    }
    window.customElements.define(custom-element, MyWebComponent);
</script>
<custom-element>
    <p slot="slot1">This is a slotted paragraph</p>
</custom-element>
<custom-element>
    <iframe slot="slot1" width="560" height="315" src="https://www.youtube.com/embed/Bv_5Zv5c-Ts" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</custom-element>

 

[HTML5] Build Flexible HTML with HTMLTemplates using Slots and Web Components

原文:https://www.cnblogs.com/Answer1215/p/10360218.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!