小胡子模板不会在表格内呈现 [英] Mustache Template doesn't render inside table tbody

查看:80
本文介绍了小胡子模板不会在表格内呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么相同的JSON目标代码使用 ul 元素生成输出,但不生成标记。

Why is that the same JSON object code generates the output with ul elements, but not with a table tag.

我有我的胡子模板,如:

I have my Mustache template like:

<div id="template-ul">
    <h3>{{name}}</h3>
    <ul>
        {{#students}}
        <li>{{name}} - {{age}}</li>
        {{/students}}
    </ul>
</div>

<div id="template-table">
    <table>
        <thead>
            <th>Name</th>
            <th>Age</th>
        </thead>
        <tbody>
        {{#students}}
            <tr>
                <td>{{name}}</td>
                <td>{{age}}</td>
            </tr>
        {{/students}}
        </tbody>
    </table>
</div>

以下是javascript代码:

Here is the javascript code:

var testing = {
    "name" : "student-collection",
    "students" : [
        {
            "name" : "John",
            "age" : 23
        },
        {
            "name" : "Mary",
            "age" : 21
        }
    ]
};

var divUl = document.getElementById("template-ul");
var divTable = document.getElementById("template-table");

divUl.innerHTML = Mustache.render(divUl.innerHTML, testing);
divTable.innerHTML = Mustache.render(divTable.innerHTML, testing);

以下是jsFiddle上的代码 - http://jsfiddle.net/pRSjH/2/

Here is the code on jsFiddle - http://jsfiddle.net/pRSjH/2/

推荐答案

divTable.innerHTML 返回而不是正确的模板。
可能是因为浏览器试图呈现无效的HTML。我认为您可以将模板放入< script> 标记来解决此问题(小提琴

divTable.innerHTML returns this instead of correct template. Probably it's happens because browser tries to render invalid HTML. I think you can put your template into <script> tag to solve this problem (the fiddle)

这篇关于小胡子模板不会在表格内呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆