简体   繁体   English

隐藏/显示切换按钮不以隐藏内容开头

[英]Hide / Show toggle button doesn't start with content hidden

It works but it doesn't start off with my 'more details' content toggled to hidden.它可以工作,但它并没有从我的“更多详细信息”内容切换到隐藏开始。 It starts off with the content showing.它从显示的内容开始。 I have rewritten this 10 times and can't figure it out.我已经重写了10次,无法弄清楚。

 function toggle() { let Text = document.getElementById('moreDetails'); if (Text.style.display == "none") { Text.style.display = "block"; } else { Text.style.display = "none"; } }
 <div id="moreDetails"> <h3> 001 </h3> <h3> Saturate Radio </h3> <h4> N00DS </h4> </div> <button title="Click to Show" type="button" onclick="toggle()">MoreDetails</button>

add style display none to your div id moreDetails将样式显示无添加到您的 div id 更多详细信息

<div id="moreDetails" style="display: none">

or you can use like this also或者你也可以这样使用

<script>
function toggle(){
    let Text = document.getElementById('moreDetails');

    if(Text.style.display == "none"){
        Text.style.display= "block";
    }
    else {
        Text.style.display = "none";
    }
}
document.getElementById("moreDetails").style.display = "none";
 
</script>

or you can use ternary operator或者你可以使用三元运算符

<script>
    function toggle(){
        const Text = document.getElementById('moreDetails');
        Text.style.display = Text.style.display == "none" ? "block" : "none";
    }
    document.getElementById("moreDetails").style.display = "none";
</script>

To start with more details hidden, you should add style="display: none;"要开始隐藏更多细节,您应该添加style="display: none;" to your div;到你的 div; Here's the code:这是代码:

 function toggle() { let Text = document.getElementById('moreDetails'); if (Text.style.display == "none") { Text.style.display = "block"; } else { Text.style.display = "none"; } }
 <div id="moreDetails" style="display: none;"> <h3> 001 </h3> <h3> Saturate Radio </h3> <h4> N00DS </h4> </div> <button title="Click to Show" type="button" onclick="toggle()">MoreDetails</button>

Add display none for div.为 div 添加无显示。

 function toggle(){ const Text = document.getElementById('moreDetails'); Text.style.display = Text.style.display == "none"? "block": "none"; }
 <div id="moreDetails" style="display: none"> <h3> 001 </h3> <h3> Saturate Radio </h3> <h4> N00DS </h4> </div> <button title="Click to Show" type="button" onclick="toggle()">MoreDetails</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM