JS获取具有表内容的HTML`<template>`innerHTML失败

时间:2019-11-09 02:14:33

标签: javascript html templates

在HTML文件中,我具有以下内容:

<template id="notWorking">
    <table class="table table-xs table-hover">
      <thead>
        <tr>
          <th>Conteneur</th>
        </tr>
      </thead>
      <tbody>
        {{ each(options.dossier.conteneurs) }}
          <tr>
            <td>{{ @this.numConteneur }}</td>
          </tr>
        {{ /each }}
      </tbody>
    </table>
</template>

但是当我致电console.log(document.getElementById("notWorking").innerHTML)时,我得到以下信息:


{{ each(options.dossier.conteneurs) }}

{{ /each }}
<table class="table table-xs table-hover">
<thead>
<tr>
<th>Conteneur</th>
</tr>
</thead>
<tbody><tr>
<td>{{ @this.numConteneur }}</td>
</tr></tbody>
</table>

似乎该元素未正确解析...?我做错了什么吗?

请参阅:https://codepen.io/nebrelbug/pen/OJJEvmW

1 个答案:

答案 0 :(得分:1)

<tbody>只能将<tr>作为孩子。不允许输入文字内容。