admin管理员组文章数量:1357387
I have a javascript function where i return a html code. In that piece of return statement i want to use if conditions. Please guide me how to use that.
This is my javascript function:
function abc(param)
{
var step =2;
if(n>0){
return `
<div>
**{% if step == '1' %}
<div class="box" style="background-color:red" id="prodcolor">
<span style="display:none"> Sample</span>
</div>
{% endif %}**
</div>
`;
}
}
I have highlighted the place where i have used if statement. This is my javascript file.
I have a javascript function where i return a html code. In that piece of return statement i want to use if conditions. Please guide me how to use that.
This is my javascript function:
function abc(param)
{
var step =2;
if(n>0){
return `
<div>
**{% if step == '1' %}
<div class="box" style="background-color:red" id="prodcolor">
<span style="display:none"> Sample</span>
</div>
{% endif %}**
</div>
`;
}
}
I have highlighted the place where i have used if statement. This is my javascript file.
Share Improve this question edited Mar 14, 2019 at 5:43 asked Mar 14, 2019 at 5:42 user10893802user108938022 Answers
Reset to default 2Use different strings for different conditions:
function abc(param)
{
var step =2;
if(n>0){
return step == '1' ? `
<div>
<div class="box" style="background-color:red" id="prodcolor">
<span style="display:none"> Sample</span>
</div>
</div>
`:"<div></div>"
}
}
You can use string concatenation.
Btw, I'm assuming n
is global variable here somewhere.
var n = 1
function abc(param)
{
var step = 2;
var innerHtml = '';
if(n>0){
if (step == '1') {
innerHtml = `
<div class="box" style="background-color:red" id="prodcolor">
<span style="display:none"> Sample</span>
</div>
`;
}
return `
<div>
${innerHtml}
</div>
`;
}
}
本文标签: How to return html code in a javascript function with conditional statementsStack Overflow
版权声明:本文标题:How to return html code in a javascript function with conditional statements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743957298a2568365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论