javascript jQuery 模板中的 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12366685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If statement in jQuery template
提问by bogatyrjov
The following code returns an error"Uncaught ReferenceError: size is not defined" in Chrome if variable size is not defined:
如果未定义变量大小,以下代码在 Chrome 中返回错误“ Uncaught ReferenceError: size is not defined”:
<script type="text/x-jquery-tmpl">
{{if name && size}}
<p>${name}</p>
<p>${size}</p>
{{/if}}
</script>
While this code works fine:
虽然此代码工作正常:
<script type="text/x-jquery-tmpl">
{{if name}}
{{if size}}
<p>${name}</p>
<p>${size}</p>
{{/if}}
{{/if}}
</script>
Can I somehow make it work in Chrome without using double if statementand why does it return an error at all?
我可以在不使用双 if 语句的情况下以某种方式使其在 Chrome 中工作吗,为什么它会返回错误?
回答by Mathlight
try this:
试试这个:
<script type="text/x-jquery-tmpl">
{{if name && size != null && size}}
<p>${name}</p>
<p>${size}</p>
{{/if}}
</script>
回答by www-data
Try this
试试这个
<script type="text/x-jquery-tmpl">
{{if (name != null && size != null)}}
<p>${name}</p>
<p>${size}</p>
{{/if}}
</script>
Note the space after if statement.
注意 if 语句后的空格。