如何在javascript中为类设置条件<div>



当post.title为空时,我想在引导程序中使用d-none隐藏div。到目前为止,我已经尝试过了,卡体仍然显示出来。没有文本,但我根本不想让它显示出来。一般来说,我是网络开发的新手,所以我真的不知道如何实现这一点。我也不确定我的语法是否正确。有什么建议吗?

<div class = {${post.title} == '' ? "d-none" : "d-inline"}>
<div class="card-body override-bs">
${post.title}

</div>
</div>
<div class=`${post.title === '' ? "d-none" : "d-inline"}`>
<div class="card-body override-bs">
`${post.title}`                                 
</div>
</div>

这里我使用的是Template文字和三元运算符。请查看这两个链接。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literalshttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

您不需要引导程序:

function hide(){
document.getElementById("div").setAttribute("hide","true");
}
function show(){
document.getElementById("div").setAttribute("hide","false");
}
[hide="true"]{
display: none;
}
<div hide="true" id="div"><button onclick="hide();">X</button><p>So you want to hide a div? Bootstrap not needed! :)<p><img src="https://turtleonafencepostsite.files.wordpress.com/2017/08/fullsizerender.jpg?w=648" alt="Why Turtle on a Fence Post? – Turtle on a Fence Post"/></div>
<button onclick="show();">Show it</button>

最新更新