メインコンテンツへスキップ

template要素


1.仕様

TEMPLATE要素 は、
ページの読み込み時に
すぐには描画されないものの、
後で JavaScriptを使用して
インスタンスを生成できる
HTMLを保持する
メカニズムの要素です。

template

HTMLの断片を宣言 (declare fragments of HTML)

種類
雛型要素

テンプレートコンテンツを持つことができる要素です。

使用できるコンテキスト
  • メタデータコンテンツを受け入れる要素
  • フレージングコンテンツを受け入れる要素
  • スクリプトをサポートする要素を受け入れる要素
  • colgroup要素に 0個以上

メタデータコンテンツを受け入れる要素のコンテンツとして、使用できます。
フレージングコンテンツを受け入れる要素のコンテンツとして、使用できます。
スクリプトをサポートする要素を受け入れる要素のコンテンツとして、使用できます。
colgroup要素に 0個以上を そのコンテンツとして、使用できます。

メタデータコンテンツを受け入れる要素は、head要素 です。

フレージングコンテンツを受け入れる要素は、h1要素, h2要素, h3要素, h4要素, h5要素, h6要素, p要素, pre要素, output要素, progress要素, meter要素, label要素, datalist要素, body要素, section要素, nav要素, aside要素, article要素, header要素, footer要素, main要素, address要素, li要素, dt要素, dd要素, caption要素, th要素, td要素, form要素, fieldset要素, search要素, blockquote要素, figure要素, figcaption要素, div要素, dialog要素, abbr要素, b要素, bdi要素, bdo要素, cite要素, code要素, data要素, dfn要素, em要素, i要素, kbd要素, mark要素, q要素, s要素, samp要素, small要素, span要素, strong要素, sub要素, sup要素, u要素, var要素 です。

スクリプトをサポートする要素を受け入れる要素は、hgroup要素, ol要素, ul要素, menu要素, dl要素, div要素, picture要素, table要素, tbody要素, thead要素, tfoot要素, tr要素, select要素, datalist要素, optgroup要素 です。

ただし、colgroup要素は span属性を持たない場合に限ります。

コンテンツモデル
何もない

要素のコンテンツモデルが「何もない」の場合、要素にはテキストノード (要素間の空白以外) と要素ノードが含まれていてはなりません。

template要素のテンプレートの内容は、要素自体の子ではありません。

template要素のコンテンツの位置への記述は、要素自体の子ではないのでコンテンツモデルの制限は受けません。

DOM操作の結果として、template要素にテキストノードと要素ノードを含むことも可能です。ただし、そのコンテンツモデルは「何もない」なので、いずれかを持つことはtemplate要素のコンテンツモデルに違反します。

タグの省略

どちらのタグも省略できません。

コンテンツ属性
グローバル属性 他
shadowrootmode属性
宣言型シャドウルートのストリーミングを有効にする
shadowrootdelegatesfocus属性
デリゲートのフォーカスを宣言型シャドウルートに設定します
shadowrootclonable属性
宣言型シャドウルートにクローン可能を設定します
shadowrootserializable属性
宣言型シャドウルートでシリアル化可能なセット
DOM インターフェイス
HTMLTemplateElement
  1. [Exposed=Window]
  2. interface HTMLTemplateElement : HTMLElement {
  3. [HTMLConstructor] constructor();
  4.  
  5. readonly attribute DocumentFragment content;
  6. [CEReactions] attribute DOMString shadowRootMode;
  7. [CEReactions] attribute boolean shadowRootDelegatesFocus;
  8. [CEReactions] attribute boolean shadowRootClonable;
  9. [CEReactions] attribute boolean shadowRootSerializable;
  10. };
HTMLElement
  1. [Exposed=Window]
  2. interface HTMLElement : Element {
  3. [HTMLConstructor] constructor();
  4.  
  5. // metadata attributes
  6. [CEReactions] attribute DOMString title;
  7. [CEReactions] attribute DOMString lang;
  8. [CEReactions] attribute boolean translate;
  9. [CEReactions] attribute DOMString dir;
  10.  
  11. // user interaction
  12. [CEReactions] attribute (boolean or unrestricted double or DOMString)? hidden;
  13. [CEReactions] attribute DOMString inert;
  14. undefined click();
  15. [CEReactions] attribute DOMString accessKey;
  16. readonly attribute DOMString accessKeyLabel;
  17. [CEReactions] attribute boolean draggable;
  18. [CEReactions] attribute boolean spellcheck;
  19. [CEReactions] attribute DOMString writingSuggestions;
  20. [CEReactions] attribute DOMString autocapitalize;
  21.  
  22. [CEReactions] attribute [LegacyNullToEmptyString] DOMString innerText;
  23. [CEReactions] attribute [LegacyNullToEmptyString] DOMString outerText;
  24.  
  25. ElementInternals attachInternals();
  26.  
  27. // The popover API
  28. undefined showPopover();
  29. undefined hidePopover();
  30. boolean togglePopover(optional boolean force);
  31. [CEReactions] attribute DOMString? popover;
  32. };
  33.  
  34. HTMLElement includes GlobalEventHandlers;
  35. HTMLElement includes ElementContentEditable;
  36. HTMLElement includes HTMLOrSVGElement;
Element
  1. [Exposed=Window]
  2. interface Element : Node {
  3. readonly attribute DOMString? namespaceURI;
  4. readonly attribute DOMString? prefix;
  5. readonly attribute DOMString localName;
  6. readonly attribute DOMString tagName;
  7.  
  8. [CEReactions] attribute DOMString id;
  9. [CEReactions] attribute DOMString className;
  10. [SameObject, PutForwards=value] readonly attribute DOMTokenList classList;
  11. [CEReactions, Unscopable] attribute DOMString slot;
  12.  
  13. boolean hasAttributes();
  14. [SameObject] readonly attribute NamedNodeMap attributes;
  15. sequence<DOMString> getAttributeNames();
  16. DOMString? getAttribute(DOMString qualifiedName);
  17. DOMString? getAttributeNS(DOMString? namespace, DOMString localName);
  18. [CEReactions] undefined setAttribute(DOMString qualifiedName, DOMString value);
  19. [CEReactions] undefined setAttributeNS(DOMString? namespace, DOMString qualifiedName, DOMString value);
  20. [CEReactions] undefined removeAttribute(DOMString qualifiedName);
  21. [CEReactions] undefined removeAttributeNS(DOMString? namespace, DOMString localName);
  22. [CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
  23. boolean hasAttribute(DOMString qualifiedName);
  24. boolean hasAttributeNS(DOMString? namespace, DOMString localName);
  25.  
  26. Attr? getAttributeNode(DOMString qualifiedName);
  27. Attr? getAttributeNodeNS(DOMString? namespace, DOMString localName);
  28. [CEReactions] Attr? setAttributeNode(Attr attr);
  29. [CEReactions] Attr? setAttributeNodeNS(Attr attr);
  30. [CEReactions] Attr removeAttributeNode(Attr attr);
  31.  
  32. ShadowRoot attachShadow(ShadowRootInit init);
  33. readonly attribute ShadowRoot? shadowRoot;
  34.  
  35. Element? closest(DOMString selectors);
  36. boolean matches(DOMString selectors);
  37. boolean webkitMatchesSelector(DOMString selectors); // legacy alias of .matches
  38.  
  39. HTMLCollection getElementsByTagName(DOMString qualifiedName);
  40. HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
  41. HTMLCollection getElementsByClassName(DOMString classNames);
  42.  
  43. [CEReactions] Element? insertAdjacentElement(DOMString where, Element element); // legacy
  44. undefined insertAdjacentText(DOMString where, DOMString data); // legacy
  45. };
GlobalEventHandlers
  1. interface mixin GlobalEventHandlers {
  2. attribute EventHandler onabort;
  3. attribute EventHandler onauxclick;
  4. attribute EventHandler onbeforeinput;
  5. attribute EventHandler onbeforematch;
  6. attribute EventHandler onbeforetoggle;
  7. attribute EventHandler onblur;
  8. attribute EventHandler oncancel;
  9. attribute EventHandler oncanplay;
  10. attribute EventHandler oncanplaythrough;
  11. attribute EventHandler onchange;
  12. attribute EventHandler onclick;
  13. attribute EventHandler onclose;
  14. attribute EventHandler oncontextlost;
  15. attribute EventHandler oncontextmenu;
  16. attribute EventHandler oncontextrestored;
  17. attribute EventHandler oncopy;
  18. attribute EventHandler oncuechange;
  19. attribute EventHandler oncut;
  20. attribute EventHandler ondblclick;
  21. attribute EventHandler ondrag;
  22. attribute EventHandler ondragend;
  23. attribute EventHandler ondragenter;
  24. attribute EventHandler ondragleave;
  25. attribute EventHandler ondragover;
  26. attribute EventHandler ondragstart;
  27. attribute EventHandler ondrop;
  28. attribute EventHandler ondurationchange;
  29. attribute EventHandler onemptied;
  30. attribute EventHandler onended;
  31. attribute OnErrorEventHandler onerror;
  32. attribute EventHandler onfocus;
  33. attribute EventHandler onformdata;
  34. attribute EventHandler oninput;
  35. attribute EventHandler oninvalid;
  36. attribute EventHandler onkeydown;
  37. attribute EventHandler onkeypress;
  38. attribute EventHandler onkeyup;
  39. attribute EventHandler onload;
  40. attribute EventHandler onloadeddata;
  41. attribute EventHandler onloadedmetadata;
  42. attribute EventHandler onloadstart;
  43. attribute EventHandler onmousedown;
  44. [LegacyLenientThis] attribute EventHandler onmouseenter;
  45. [LegacyLenientThis] attribute EventHandler onmouseleave;
  46. attribute EventHandler onmousemove;
  47. attribute EventHandler onmouseout;
  48. attribute EventHandler onmouseover;
  49. attribute EventHandler onmouseup;
  50. attribute EventHandler onpaste;
  51. attribute EventHandler onpause;
  52. attribute EventHandler onplay;
  53. attribute EventHandler onplaying;
  54. attribute EventHandler onprogress;
  55. attribute EventHandler onratechange;
  56. attribute EventHandler onreset;
  57. attribute EventHandler onresize;
  58. attribute EventHandler onscroll;
  59. attribute EventHandler onscrollend;
  60. attribute EventHandler onsecuritypolicyviolation;
  61. attribute EventHandler onseeked;
  62. attribute EventHandler onseeking;
  63. attribute EventHandler onselect;
  64. attribute EventHandler onslotchange;
  65. attribute EventHandler onstalled;
  66. attribute EventHandler onsubmit;
  67. attribute EventHandler onsuspend;
  68. attribute EventHandler ontimeupdate;
  69. attribute EventHandler ontoggle;
  70. attribute EventHandler onvolumechange;
  71. attribute EventHandler onwaiting;
  72. attribute EventHandler onwebkitanimationend;
  73. attribute EventHandler onwebkitanimationiteration;
  74. attribute EventHandler onwebkitanimationstart;
  75. attribute EventHandler onwebkittransitionend;
  76. attribute EventHandler onwheel;
  77. };
ElementContentEditable
  1. interface mixin ElementContentEditable {
  2. [CEReactions] attribute DOMString contentEditable;
  3. [CEReactions] attribute DOMString enterKeyHint;
  4. readonly attribute boolean isContentEditable;
  5. [CEReactions] attribute DOMString inputMode;
  6. };
HTMLOrSVGElement
  1. interface mixin HTMLOrSVGElement {
  2. [SameObject] readonly attribute DOMStringMap dataset;
  3. attribute DOMString nonce; // intentionally no [CEReactions]
  4.  
  5. [CEReactions] attribute boolean autofocus;
  6. [CEReactions] attribute long tabIndex;
  7. undefined focus(optional FocusOptions options = {});
  8. undefined blur();
  9. };
ARIA in HTML
暗黙的="No" ARIAロール="No" aria-* 属性="No"

暗黙的な ARIAセマンティクスがありませんが、意味はあり、この意味は、ARIA によって提供されず、ARIA のユーザーに公開されていないロール、状態、プロパティで表現される可能性があります。

作成者は、暗黙的な ARIAセマンティクス, または HTML要素のネイティブセマンティクスを上書きしてはいけません。

記述できる aria-* 属性はありません。

レンダリング
ボックスを生成しない
非表示の要素

CSS を大雑把に読むと、 要素とその子孫はボックスやテキストシーケンスを生成しません。 などになります。

説明
template要素は、スクリプトによって文書に複製および挿入できる HTMLのフラグメントを宣言する為に使用されます。

template要素は、ページの読み込み時にすぐには描画されないものの、後で JavaScriptを使用してインスタンスを生成できる HTMLを保持するメカニズムの要素です。

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>template要素の例 - (Example of template element) | 離珠のHTMLでしぃ!</title>
  6. <template id="itpJs001">
  7. <p>てんぷれーとのさんぷる</p>
  8. </template>
  9. <script>
  10. document.addEventListener("DOMContentLoaded", function() {
  11. document.body.appendChild(document.getElementById("itpJs001").content.cloneNode(true));
  12. });
  13. </script>
  14. </head>
  15. <body>
  16. <h1>template要素の例</h1>
  17. <p>この例の template要素は、「てんぷれーとのさんぷる」というテキストの段落を用意しています。</p>
  18. </body>
  19. </html>

このサンプルへのリンクです。
(./extemplate.htm)

JSONデータ
JSON形式での、データのイメージです。
  1. {
  2. "template": {
  3. "check": "2024-09-01",
  4. "group": "Metadata",
  5. "element": "- - EMPTY -- declare fragments of HTML. (The template element.) [Metadata|Flow|Phrasing|Script] --",
  6. "attlist": {
  7. "%Global;": "",
  8. "shadowrootmode": "CDATA #IMPLIED -- Enables streaming declarative shadow roots -- ",
  9. "shadowrootdelegatesfocus": "(shadowrootdelegatesfocus) #IMPLIED -- Sets delegates focus on a declarative shadow root -- ",
  10. "shadowrootclonable": "(shadowrootclonable) #IMPLIED -- Sets clonable on a declarative shadow root -- ",
  11. "shadowrootserializable": "(shadowrootserializable) #IMPLIED -- Sets serializable on a declarative shadow root -- ",
  12. },
  13. "comment": "HTMLの断片を宣言 (declare fragments of HTML)",
  14. "point": "スクリプトによって文書に複製および挿入できる HTMLのフラグメントを宣言する為に使用されます。",
  15. "description": "ページの読み込み時にすぐには描画されないものの、後で JavaScriptを使用してインスタンスを生成できる HTMLを保持するメカニズムの要素です。",
  16. "kind": "Template",
  17. "categories": [
  18. "Metadata",
  19. "Flow",
  20. "Phrasing",
  21. "Scriptsupporting",
  22. ],
  23. "used": [
  24. "parent (%Metadata)*",
  25. "parent (%Phrasing)*",
  26. "parent (%Scriptsupporting)*",
  27. "COLGROUP (TEMPLATE)*",
  28. ],
  29. "usedsupplement": "ただし、colgroup要素は span属性を持たない場合に限ります。",
  30. "contentmodel": "%Nothing",
  31. "contentmodelsupplement": [
  32. "template要素のテンプレートの内容は、要素自体の子ではありません。",
  33. "template要素のコンテンツの位置への記述は、要素自体の子ではないのでコンテンツモデルの制限は受けません。",
  34. "DOM操作の結果として、template要素にテキストノードと要素ノードを含むことも可能です。ただし、そのコンテンツモデルは「何もない」なので、いずれかを持つことはtemplate要素のコンテンツモデルに違反します。",
  35. ],
  36. "tag": [
  37. "-",
  38. "-",
  39. ],
  40. "attributes": {
  41. "shadowrootmode": "宣言型シャドウルートのストリーミングを有効にする",
  42. "shadowrootdelegatesfocus": "デリゲートのフォーカスを宣言型シャドウルートに設定します",
  43. "shadowrootclonable": "宣言型シャドウルートにクローン可能を設定します",
  44. "shadowrootserializable": "宣言型シャドウルートでシリアル化可能なセット",
  45. },
  46. "dominterface": "HTMLTemplateElement",
  47. "rendering": "Hidden",
  48. "reference": [
  49. {
  50. "title": "HTML Living Standard. 4.12.3 The template element.",
  51. "href": "https://html.spec.whatwg.org/multipage/scripting.html#the-template-element",
  52. "heading": "HTML Living Standard. 4.12.3 The template element",
  53. "sub": "Last Updated 27 August 2024",
  54. },
  55. {
  56. "title": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements.",
  57. "href": "https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements",
  58. "heading": "HTML Living Standard. 15 Rendering 15.3.1 Hidden elements",
  59. "sub": "Last Updated 27 August 2024",
  60. },
  61. {
  62. "title": "HTML 5.2 W3C Recommendation. 4.12.3 The template element. W3C Recommendation, 14 December 2017.",
  63. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/semantics-scripting.html#the-template-element",
  64. "heading": "HTML 5.2 W3C 勧告. 4.12.3 The template element",
  65. "sub": "W3C勧告、2017年12月14日",
  66. },
  67. {
  68. "title": "HTML 5.2 W3C Recommendation. 10.3.1. Hidden elements.",
  69. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/rendering.html#hidden-elements",
  70. "heading": "HTML 5.2 W3C 勧告. 10.3.1. Hidden elements",
  71. "sub": "W3C勧告、2017年12月14日",
  72. },
  73. {
  74. "title": "HTML 5.1 W3C Recommendation. 4.12.3 The template element. W3C Recommendation, 1 November 2016.",
  75. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/semantics-scripting.html#the-template-element",
  76. "heading": "HTML 5.1 W3C 勧告. 4.12.3 The template element",
  77. "sub": "W3C勧告、2016年11月1日",
  78. },
  79. {
  80. "title": "HTML 5.1 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 1 November 2016.",
  81. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/rendering.html#hidden-elements",
  82. "heading": "HTML 5.1 W3C 勧告. 10.3.1. Hidden elements",
  83. "sub": "W3C勧告、2016年11月1日",
  84. },
  85. {
  86. "title": "HTML 5 W3C Recommendation. 4.12.3 The template element. W3C Recommendation, 28 October 2014.",
  87. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/scripting-1.html#the-template-element",
  88. "heading": "HTML 5 W3C 勧告. 4.12.3 The template element",
  89. "sub": "W3C勧告、2014年10月28日",
  90. },
  91. {
  92. "title": "HTML 5 W3C Recommendation. 10.3.1. Hidden elements. W3C Recommendation, 28 October 2014.",
  93. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/rendering.html#hidden-elements",
  94. "heading": "HTML 5 W3C 勧告. 10.3.1. Hidden elements",
  95. "sub": "W3C勧告、2014年10月28日",
  96. },
  97. {
  98. "title": "MDN Web Docs. HTML <template>: The Content Template element.",
  99. "href": "https://developer.mozilla.org/ja/docs/Web/HTML/Element/template",
  100. "heading": "<template>: コンテンツテンプレート要素 - HTML: ハイパーテキストマークアップ言語 | MDN",
  101. },
  102. ],
  103. "version": {
  104. "first": "HTML5",
  105. "latest": "HTML Living Standard",
  106. },
  107. }
  108. }

注意: 簡易に描画する為に、各項目の後ろに , を付与しています。JSON の形式では、末尾の項目の後には , は付けません。

最終確認日

2024-09-01


2.使用できるコンテキスト

template要素 は、
メタデータコンテンツ,
フレージングコンテンツ,
スクリプトをサポートする要素を
受け入れる要素と
span属性を持たない
colgroup要素に
配置できます。

template要素が使用できるコンテキスト(Context文脈)は、メタデータコンテンツ, フレージングコンテンツ, scriptコンテンツを受け入れる要素と、span属性を持たない colgroup要素コンテンツとしてです。

document
html
head
body
template
p
hgroup
template
template

template要素は、html要素に配置できます。

template要素は、フレージングコンテンツコンテンツの一部として配置できる要素であるp要素に配置できます。

template要素は、scriptコンテンツコンテンツの一部として配置できる要素であるhgroup要素に配置できます。


3.コンテンツモデル

template要素 は、
制限はありません。

template要素子や子孫としてどのようなコンテンツ(Content内容) が含まれなければならないかというと、何もない」 (要素にはテキストノード(要素間の空白以外) と要素ノードが含まれていてはなりません。) です。

これで注意してほしいのは、これはコンテンツモデルであって、template要素を持たないという事です。そしてtemplate要素開始タグtemplate要素終了タグの間に記述される物は、template要素content IDL属性によって返されるDocumentFragment子や子孫です。

template要素は、JavaScript を使用してインスタンスを生成する為の雛型なので、生成先で適切である必要はありますが記述の時点では制限はありません。 また、これを書いてて思ったのが、template要素template要素の入れ子は、意味としても変だし、記述的にも終了タグの判断がどうなるか。

作成者のイメージとしては、template要素のコンテンツとして自由に要素が配置できる感じです。
ただ、内部処理としては、別の箇所に子要素を保持している様です。

尚、template要素コンテンツモデルNothing(何もない) です。コンテンツモデルが「何もない」の場合、要素にはテキストノード (要素間の空白以外) と要素ノードが含まれていてはなりません。
つまり、子の位置に記述するのは制限もなく自由であるが、スクリプトなどで要素にテキストノード, 要素ノードを含めてはいけないという事です。


4.コンテンツ属性

template要素 は、
グローバル属性と、
shadowrootmode,
shadowrootdelegatesfocus,
shadowrootclonable,
shadowrootserializable を
設定できます。

template要素に指定することができる属性は、グローバル属性shadowrootmode属性, shadowrootdelegatesfocus属性, shadowrootclonable属性, shadowrootserializable属性 を設定できます。

shadowrootmode属性

  1. <template shadowrootmode="open" shadowrootdelegatesfocus>

shadowrootmode属性は、ストリーミング宣言型シャドウ ルートを有効にします。OPEN, closed があり、それぞれ「オープン宣言型シャドウルート」, 「閉じられた宣言型シャドウルート」を表します。
無効な値のデフォルトと欠落値のデフォルトは両方とも none 状態です。

shadowrootdelegatesfocus属性

  1. <template shadowrootmode="open" shadowrootdelegatesfocus>

shadowrootdelegatesfocus属性は、デリゲートのフォーカスを宣言型シャドウルートに設定するブール属性です。

shadowrootclonable属性

  1. <template shadowrootmode="open" shadowrootdelegatesfocus shadowrootclonable>

shadowrootclonable属性は、宣言型シャドウルートにクローン可能を設定するブール属性です。

shadowrootserializable属性

  1. <template shadowrootmode="open" shadowrootdelegatesfocus shadowrootserializable>

shadowrootserializable属性は、宣言的シャドウルートにシリアライズ可能を設定するブール属性です。


5.DOM インターフェイス

template要素
直接表す
プロパティは
ありません。

template要素を JavaScript などで操作するには、documentgetElementById()メソッドを用いてid属性で指定して、template要素を取得する方法があります。この時に取得できる値の形式はHTMLTemplateElement インターフェイスです。

template要素に記述した内容を取得するには、HTMLTemplateElement インターフェイスcontent プロパティ を用います。

template要素を用いなくても、スクリプトのdocument.createElement() メソッドdocument.createTextNode() メソッドを用いて同等の事が出来ます。 違いは、HTMLで情報を保持するかスクリプトで保持するかだと思います。


  1. <template id="itpJs501">
  2. <p>テンプレートのサンプル</p>
  3. </template>
  4. <script>
  5. document.addEventListener("DOMContentLoaded", function() {
  6. document.getElementById("idvJs502").appendChild(document.getElementById("itpJs501").content.cloneNode(true));
  7. });
  8. </script>
  9. <hr>
  10. <div id="idvJs502">
  11. </div>

template要素から、雛型を取り出す例です。


  1. <script>
  2. document.addEventListener("DOMContentLoaded", function() {
  3. document.getElementById("idvJs503").appendChild(document.createElement("p").appendChild(document.createTextNode("別の方法")));
  4. });
  5. </script>
  6. <hr>
  7. <div id="idvJs503">
  8. </div>

template要素での処理と同等の事を、スクリプトだけで行う例です。


6.レンダリング

  1. template {
  2. display: none;
  3. }

template要素ユーザーエージェントレベルスタイルシートデフォルトでは、displayプロパティの値は none であり、この要素とその子孫はボックステキストシーケンスを生成しません。

雛型をコンテナする要素なので、ビジュアルメディアでボックスを生成する様な指定は行わないのが良いでしょう。


7.気を付ける点

コンテンツモデル

template要素コンテンツモデルは、「何もありません」(Nothing) です。template要素のテンプレートの内容は、要素自体の子ではありません。

技術的に DOM操作によってtemplate要素にテキストノード, 要素ノードを含める事が出来ますが、それは上記の様にtemplate要素コンテンツモデルから不適切です。

尚 HTML文書の template要素に記述した内容を取得するには、HTMLTemplateElement インターフェイスcontent プロパティ を用います。


8.付記