template标签介绍和使用

template标签介绍和使用

1.介绍template标签是html5新出来的标签,具有3个特点,(1)随意性可以写在页面中的任何地方、(2)不可见性它里面的元素都是不可见的、(3)页面也不会渲染它

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

    <-- 定义模板和内容,可以定义在任何地方 -->
  <template id="temp">
    <div>你好模板</div>
  </template>
</head>
<body>
  <div id="root"></div>
</body>
<script>

    <!-- 通过js来动态渲染该模板中的内容  -->
  const fragment = document.getElementById('temp').content.cloneNode(true)
  document.getElementById('root').appendChild(fragment)
</script>
</html>
原文地址:https://www.cnblogs.com/zxuedong/p/12978300.html