常用的html標(biāo)簽匯總、以及操作過(guò)程中的一些bug問(wèn)題解決方法,以下龍騰飛網(wǎng)絡(luò)科技-小吳在建站實(shí)操中筆記記錄,一路走來(lái),一步步學(xué)習(xí)、總結(jié)、整理的一些資料,不用死記硬背,保存使用非常方便,實(shí)操過(guò)程中遇到了就查詢(xún)搜索一下,實(shí)踐出真章,做多了自然就熟悉了:
【定義和用法】
template標(biāo)簽用作容器,用于在頁(yè)面加載時(shí)對(duì)用戶(hù)隱藏一些 HTML 內(nèi)容。
template內(nèi)部的內(nèi)容可以在稍后使用 JavaScript 呈現(xiàn)。
如果您有一些希望重復(fù)使用的 HTML 代碼,但在需要時(shí)才顯示出來(lái),您可以使用 template標(biāo)簽。如果沒(méi)有 template標(biāo)簽,您需要使用 JavaScript 創(chuàng)建 HTML 代碼來(lái)防止瀏覽器呈現(xiàn)該代碼。
【實(shí)例】
例子 1
使用 template來(lái)保存一些在頁(yè)面加載時(shí)將被隱藏的內(nèi)容。并使用 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
為數(shù)組中的每一項(xiàng)使用一個(gè)新的 div 元素填充網(wǎng)頁(yè)。每個(gè) 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
檢查瀏覽器對(duì) template的支持:
<script>if (document.createElement("template").content) { document.write("Your browser supports template!");} else { document.write("Your browser does not supports template!");}</script>