如果 undefined variable 大小,以下代码在 Chrome 中返回错误“Uncaught ReferenceError: size is not defined”:
<script type="text/x-jquery-tmpl">
{{if name && size}}
<p>${name}</p>
<p>${size}</p>
{{/if}}
</script>
虽然此代码工作正常:
<script type="text/x-jquery-tmpl">
{{if name}}
{{if size}}
<p>${name}</p>
<p>${size}</p>
{{/if}}
{{/if}}
</script>
我能否以某种方式让它在 Chrome 中工作而不使用双 if 语句,为什么它会返回错误?
最佳答案
试试这个:
<script type="text/x-jquery-tmpl">
{{if name && size != null && size}}
<p>${name}</p>
<p>${size}</p>
{{/if}}
</script>
关于javascript - jQuery 模板中的 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12366685/