{{HTMLRef}}
HTML 内容模板(<template>)元素是一种用于保存客户端内容机制,该内容在加载页面时不会呈现,但随后可以 (原文为 may be) 在运行时使用 JavaScript 实例化。
将模板视为一个可存储在文档中以便后续使用的内容片段。虽然解析器在加载页面时确实会处理<template>元素的内容,但这样做只是为了确保这些内容有效;但元素内容不会被渲染。
| Content categories | Metadata content, flow content, phrasing content, script-supporting element |
|---|---|
| Permitted content | No restrictions |
| Tag omission | {{no_tag_omission}} |
| Permitted parents | {{HTMLElement("body")}}, {{HTMLElement("frameset")}}, {{HTMLElement("head")}}, {{HTMLElement("dl")}} and {{HTMLElement("colgroup")}} without a span attribute |
| Permitted ARIA roles | None |
| DOM interface | {{domxref("HTMLTemplateElement")}} |
此元素仅包含全局属性。
但, {{domxref("HTMLTemplateElement")}} 有个属性: {{domxref("HTMLTemplateElement.content", "content")}} , 这个属性是只读的{{domxref("DocumentFragment")}} 包含了模板所表示的 DOM 树。
首先我们从示例的 HTML 部分开始。
<table id="producttable">
<thead>
<tr>
<td>UPC_Code</td>
<td>Product_Name</td>
</tr>
</thead>
<tbody>
<!-- 现有数据可以可选地包括在这里 -->
</tbody>
</table>
<template id="productrow">
<tr>
<td class="record"></td>
<td></td>
</tr>
</template>
首先,我们有一个表,稍后我们将使用 JavaScript 代码在其中插入内容。然后是模板,它描述了表示单个表行的 HTML 片段的结构。
既然已经创建了表并定义了模板,我们使用 JavaScript 将行插入到表中,每一行都是以模板为基础构建的。
// 通过检查来测试浏览器是否支持 HTML 模板元素
// 用于保存模板元素的内容属性。
if ('content' in document.createElement('template')) {
// 使用现有的 HTML tbody 实例化表和该行与模板
let t = document.querySelector('#productrow'),
td = t.content.querySelectorAll("td");
td[0].textContent = "1235646565";
td[1].textContent = "Stuff";
// 克隆新行并将其插入表中
let tb = document.getElementsByTagName("tbody");
let clone = document.importNode(t.content, true);
tb[0].appendChild(clone);
// 创建一个新行
td[0].textContent = "0384928528";
td[1].textContent = "Acme Kidney Beans";
// 克隆新行并将其插入表中
let clone2 = document.importNode(t.content, true);
tb[0].appendChild(clone2);
} else {
// 找到另一种方法来添加行到表,因为不支持 HTML 模板元素。
}
结果是原始的 HTML 表格,通过 JavaScript 添加了两行新内容:
table {
background: #000;
}
table td {
background: #fff;
}
{{EmbedLiveSample("示例", 500, 120)}}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('HTML WHATWG','/scripting-1.html#the-template-element','template element')}} | {{Spec2('HTML WHATWG')}} | |
| {{SpecName('HTML5 W3C','/scripting-1.html#the-template-element','template element')}} | {{Spec2('HTML5 W3C')}} | Initial definition |
{{Compat("html.elements.template")}}