常用的html标签汇总、以及操作过程中的一些bug问题解决方法,以下龙腾飞网络科技-小吴在建站实操中笔记记录,一路走来,一步步学习、总结、整理的一些资料,不用死记硬背,保存使用非常方便,实操过程中遇到了就查询搜索一下,实践出真章,做多了自然就熟悉了:
【定义和用法】
template标签用作容器,用于在页面加载时对用户隐藏一些 HTML 内容。
template内部的内容可以在稍后使用 JavaScript 呈现。
如果您有一些希望重复使用的 HTML 代码,但在需要时才显示出来,您可以使用 template标签。如果没有 template标签,您需要使用 JavaScript 创建 HTML 代码来防止浏览器呈现该代码。
【实例】
例子 1
使用 template来保存一些在页面加载时将被隐藏的内容。并使用 JavaScript 显示它:
<button onclick="showContent()">Show hidden content</button><template> <h2>Flower</h2> <img src="img_white_flower.jpg" width="214" height="204"></template><script>function showContent() { var temp = document.getElementsByTagName("template")[0]; var clon = temp.content.cloneNode(true); document.body.appendChild(clon);}</script>
例子 2
为数组中的每一项使用一个新的 div 元素填充网页。每个 div 元素的 HTML 代码都在 template 元素中:
<template> <div class="myClass">I like: </div></template><script>var myArr = ["Audi", "BMW", "Ford", "Honda", "Jaguar", "Nissan"];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>
例子 3
检查浏览器对 template的支持:
<script>if (document.createElement("template").content) { document.write("Your browser supports template!");} else { document.write("Your browser does not supports template!");}</script>