HTML
tag
Example
Use
When the page loads, the content inside this tag is not displayed; after loading, you can use JavaScript to reveal it:
<
button
onclick
=
"
showContent()
"
Show Hidden Content
button
<
template
<
h2
logo
h2
<
img
src
=
"
https://static.jyshare.com/images/runoob-logo.png
"
template
<
script
function showContent() { var temp = document.getElementsByTagName("template")[0]; var clon = temp.content.cloneNode(true); document.body.appendChild(clon); }
script
Try it »
Browser support
The numbers in the table indicate the version of the first browser that supported the element.
Element
26.0
13.0
22.0
8.0
15.0
Tag Definition and Usage Notes
The tag defines some content that is hidden when the page loads; the content inside it can be presented later using JavaScript.
If you have some HTML code that needs to be reused, you can use to set it as a common template.
More examples
Example
Each array element in the example uses a new div element to populate the page. The HTML code for each div element is inside the template element::
<
template
<
div
class
=
"
myClass
"
I like:
div
template
<
script
var myArr = ["Google", "Runoob", "Taobao", "Wiki", "Zhihu", "Baidu"]; function showContent() { var temp, item, a, i; temp = document.getElementsByTagName("template")[0]; item = temp.content.querySelector("div"); for (i = 0; i
<
myArr
.
length
;
i
++) {
a
=
document
.
importNode
(
item
,
true
);
a
.
textContent
+=
myArr
[
i
];
document
.
body
.
appendChild
(
a
); } }
script
Try it »
Example
Check whether the browser supports the template tag:
if
(
document
.
createElement
(
"
template
"
)
.
content
)
{
document
.
write
(
"
Your browser supports the template tag!
"
)
;
}
else
{
document
.
write
(
"
Your browser does not support the template tag!
"
)
;
}
Try it »
Global Attributes
<
button
onclick
=
"
showContent()
"
Show Hidden Content
button
<
template
<
h2
logo
h2
<
img
src
=
"
https://static.jyshare.com/images/runoob-logo.png
"
template
<
scriptfunction showContent() { var temp = document.getElementsByTagName("template")[0]; var clon = temp.content.cloneNode(true); document.body.appendChild(clon); }
script
Try it »
Browser support
The numbers in the table indicate the version of the first browser that supported the element.
| Element | |||||
|---|---|---|---|---|---|
| 26.0 |
13.0 |
22.0 |
8.0 |
15.0 |
Tag Definition and Usage Notes
The tag defines some content that is hidden when the page loads; the content inside it can be presented later using JavaScript.
If you have some HTML code that needs to be reused, you can use Each array element in the example uses a new div element to populate the page. The HTML code for each div element is inside the template element:: I like:
div
template < script var myArr = ["Google", "Runoob", "Taobao", "Wiki", "Zhihu", "Baidu"]; function showContent() { var temp, item, a, i; temp = document.getElementsByTagName("template")[0]; item = temp.content.querySelector("div"); for (i = 0; i < myArr . length ; i ++) { a = document . importNode ( item , true ); a . textContent += myArr [ i ]; document . body . appendChild ( a ); } }
script Try it » Check whether the browser supports the template tag: if ( document . createElement ( " template " ) . content ) { document . write ( " Your browser supports the template tag! " ) ; } else { document . write ( " Your browser does not support the template tag! " ) ; } Try it » |