HTML5模板标记代码组织

时间:2015-08-13 13:47:24

标签: html5 templatetag

最好是在头部注入所有模板标签吗?

我认为这样代码会更清晰,它可以在可以渲染的项目和其他不可渲染的项目之间拆分元素。 你觉得怎么样或者你自己用了什么?

1 个答案:

答案 0 :(得分:0)

它确实基于个人偏好和模板的上下文。 template标记当然可以放在head元素中,W3 standard allows metadata content theretemplate元素can indeed be metadata。但是,the W3 code example(转载如下)显示它位于拟使用的位置附近。

<!DOCTYPE html>
<title>Cat data</title>
<script>
 // Data is hard-coded here, but could come from the server
 var data = [
   { name: 'Pillar', color: 'Ticked Tabby', sex: 'Female (neutered)', legs: 3 },
   { name: 'Hedral', color: 'Tuxedo', sex: 'Male (neutered)', legs: 4 },
 ];
</script>
<table>
 <thead>
  <tr>
   <th>Name <th>Color <th>Sex <th>Legs
 <tbody>
  <template id="row">
   <tr><td><td><td><td>
  </template>
</table>
<script>
 var template = document.querySelector('#row');
 for (var i = 0; i < data.length; i += 1) {
   var cat = data[i];
   var clone = template.content.cloneNode(true);
   var cells = clone.querySelectorAll('td');
   cells[0].textContent = cat.name;
   cells[1].textContent = cat.color;
   cells[2].textContent = cat.sex;
   cells[3].textContent = cat.legs;
   template.parentNode.appendChild(clone);
 }
</script>
相关问题