[英]How to avoid Same html code in Section tag?
I have 3 sections with the same html code inside, how can I avoid that, is there a way to just give them a piece of html code or as a code template by html,javascript...etc Thanks, with regards. I have 3 sections with the same html code inside, how can I avoid that, is there a way to just give them a piece of html code or as a code template by html,javascript...etc Thanks, with regards.
<section class="Movie-list-1">
<h1> New movies </h1>
<!-- 10 divs here-->
</section>
<section class="Movie-list-2">
<h1> 4k movies </h1>
<!-- 10 divs here -->
</section>
<section class="Movie-list-3">
<h1> New movies </h1>
<!-- 10 divs here -->
</section>
You can create a <template>
and reuse it in a custom element with slots :您可以创建一个
<template>
并在带有 slot 的自定义元素中重用它:
customElements.define('movie-list', class MovieList extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }).appendChild(document.getElementById('movie-list').content.cloneNode(true)); } })
<template id="movie-list"> <section class="movie-list"> <h1><slot name="title">New movies</slot></h1> <slot name="divs"></slot> </section> </template> <movie-list> <span slot="title">New Movies</span> <div slot="divs"> <div>Foo</div> <div>Bar</div> <div>Baz</div> </div> </movie-list> <movie-list> <span slot="title">Top Movies</span> <div slot="divs"> <div>Foo??</div> <div>Bar?!</div> <div>Baz!?</div> </div> </movie-list>
This is what it looks like in the DOM:这是它在 DOM 中的样子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.