Mustache 模板不会在表格 tbody 内呈现 [英] Mustache Template doesn't render inside table tbody
问题描述
为什么相同的 JSON 对象代码生成带有 ul
元素的输出,而不是带有 table
标记的输出.
我的 Mustache 模板如下:
<h3>{{name}}</h3><ul>{{#学生们}}<li>{{name}} - {{age}}</li>{{/学生们}}<div id="模板表"><表格><头><th>姓名</th><th>年龄</th></thead>
{{#学生们}}<tr><td>{{name}}</td><td>{{age}}</td></tr>{{/学生们}}</tbody>这是javascript代码:
var 测试 = {"name" : "学生收藏",学生们" : [{"name" : "约翰",年龄":23},{"name" : "玛丽",年龄":21}]};var divUl = document.getElementById("template-ul");var divTable = document.getElementById("模板表");divUl.innerHTML = Mustache.render(divUl.innerHTML, testing);divTable.innerHTML = Mustache.render(divTable.innerHTML, testing);
这是jsFiddle上的代码 - http://jsfiddle.net/pRSjH/2/
解决方案 divTable.innerHTML
返回 this 而不是正确的模板.可能是因为浏览器试图呈现无效的 HTML.我想你可以把你的模板放到
标签中来解决这个问题(小提琴)
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>
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);
Here is the code on jsFiddle - http://jsfiddle.net/pRSjH/2/
解决方案 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)
这篇关于Mustache 模板不会在表格 tbody 内呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆