我正在使用blueimp-tmpl编写一个javascript模板,当我使用下面的代码时,结果是正确的:
<script type="text/x-tmpl" id="tmpl-posts">
{% for (var i=0;i<o.content.length;i++) {
var item=o.content[i];
%}
<tr>
<td><a href="postDetail.html?id={%=item.id%}">{%=item.title%}</a></td>
<td>{%=item.posterNickName%}</td>
<td>{%=item.createTime%}</td>
<td>{%=item.lastUpdateTime%}</td>
<td>{%=item.replyCount%}</td>
{% if(item.posterId==1&&!item.locked){ %}
<td><a href="updatePost.html?id={%=item.id%}">修改</a></td>
<td><a href="javascript:del('{%=item.id%}')">删除</a></td>
{% }else{ %}
<td></td>
<td></td>
{% } %}
</tr>
{% } %}
</script>
但是当我将for循环更改为“for in”时,结果为空,并且chrome-devtools报告错误,然后我刷新页面,错误消失,结果仍然为空。
<script type="text/x-tmpl" id="tmpl-posts">
{% for (var item in o.content){ %}
<tr>
<td><a href="postDetail.html?id={%=item.id%}">{%=item.title%}</a></td>
<td>{%=item.posterNickName%}</td>
<td>{%=item.createTime%}</td>
<td>{%=item.lastUpdateTime%}</td>
<td>{%=item.replyCount%}</td>
{% if(item.posterId==1&&!item.locked){ %}
<td><a href="updatePost.html?id={%=item.id%}">修改</a></td>
<td><a href="javascript:del('{%=item.id%}')">删除</a></td>
{% }else{ %}
<td></td>
<td></td>
{% } %}
</tr>
{% } %}
</script>
最佳答案
我也不知道为什么,但你可以这样做。 修改这一行:
{% for (var item in o.content){ %}
至:
{% for (var i=0, item; o.content[i]; i++){ %}
关于javascript - 我无法在 blueimp/JavaScript-Templates 中使用 javascript "for in"语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38349022/