简体   繁体   English

如何制作 <div> 消失然后重新出现onclick

[英]How to make a <div> disappear then reappear onclick

I'm trying to make a FAQ section for my website but I can't seem to get this simple code to work, I've been stomped for hours, I'm trying to make a box disappear onclick , then reappear. 我正在尝试为我的网站创建一个“常见问题解答”部分,但似乎无法使这个简单的代码正常工作,我已经踩了好几个小时了,我试图让一个框消失onclick ,然后重新出现。 I've gotten it to disappear but when I add in the reappear the whole thing breaks. 我已经消失了,但是当我重新出现时,整个事情就坏了。 Here's my code: 这是我的代码:

 function showDiv() { document.getElementById('FAQPage').style.display = "block"; } function closeDiv() { document.getElementById('FAQPage').style.display = "none"; } function showOrHide() { if (document.getElementById('FAQPage').style.display = "block") { closeDiv() } else { showDiv() } } 
 #FAQPage { background-color: #F8F8F8; width: 50%; position: absolute; left: 41%; height: auto; top: 10%; display: none; } 
 <div id="FAQSidebar-buttons" onclick="showOrHide()"> <h2 align="center">What kind of websites can we do?</h2> </div> <div id="FAQPage"> <h1 align="center">What kind of websites can we do?</h1> <hr /> <p align="center">Here at The Castle, when it comes to web design we can do almost anything! We can do Ecommerce, mobile websites, personal websites, and much more! Here, no matter what kind of business you have, you can get a site for it.</p> </div> 

You have typo: 您有错别字:

if(document.getElementById('FAQPage').style.display == "block") // double == should be used here

Updated Code : 更新的代码

 function showDiv() { document.getElementById('FAQPage').style.display = "block"; } function closeDiv() { document.getElementById('FAQPage').style.display = "none"; } function showOrHide() { if(document.getElementById('FAQPage').style.display == "block") { closeDiv() } else { showDiv() } } 
 #FAQPage{ background-color:#F8F8F8; width:50%; position:absolute; left:41%; height:auto; top:10%; display:none; } 
 <div id="FAQSidebar-buttons" onclick="showOrHide()"> <h2 align="center">What kind of websites can we do?</h2> </div> <div id="FAQPage"> <h1 align="center">What kind of websites can we do?</h1><hr /> <p align="center">Here at The Castle, when it comes to web design we can do almost anything! We can do Ecommerce, mobile websites, personal websites, and much more! Here, no matter what kind of business you have, you can get a site for it.</p> </div> 

PS: usually showOrHide is called toggle (ex: toggleVisibility) PS:通常将showOrHide称为toggle (例如:toggleVisibility)

You need to fix the = in your condition, use === instead, the first will assign, the second will check. 您需要根据情况修复= ,使用===代替,第一个将分配,第二个将检查。

And instead of using document.getElementById each time, use a variable to store it to prevent querying the DOM each time. 并且不要使用每次存储document.getElementById的变量,而是使用变量来存储它以防止每次查询DOM

 var faqPage = document.getElementById('FAQPage'); function showDiv() { faqPage.style.display = "block"; } function closeDiv() { faqPage.style.display = "none"; } function showOrHide() { if (faqPage.style.display === "block") { closeDiv() } else { showDiv() } } 
 #FAQPage { background-color: #F8F8F8; width: 50%; position: absolute; left: 41%; height: auto; top: 10%; display: none; } 
 <div id="FAQSidebar-buttons" onclick="showOrHide()"> <h2 align="center">What kind of websites can we do?</h2> </div> <div id="FAQPage"> <h1 align="center">What kind of websites can we do?</h1> <hr /> <p align="center">Here at The Castle, when it comes to web design we can do almost anything! We can do Ecommerce, mobile websites, personal websites, and much more! Here, no matter what kind of business you have, you can get a site for it.</p> </div> 

May be you need to change assignment operator to operator of equivalence 可能是您需要将赋值运算符更改为等效运算符

if(document.getElementById('FAQPage').style.display = "block") {

to: 至:

if(document.getElementById('FAQPage').style.display == "block") {

Use a class for clarity like: 为清晰起见使用类,例如:

 function showOrHide(){ var faq = document.getElementById("FAQPage"); faq.classList.toggle("showed"); } 
 #FAQPage{ background-color:#F8F8F8; width:50%; position:absolute; left:41%; height:auto; top:10%; display: none } #FAQPage.showed{ display: block; } 
 <div id="FAQSidebar-buttons" onclick="showOrHide()"> <h2 align="center">What kind of websites can we do?</h2> </div> <div id="FAQPage"> <h1 align="center">What kind of websites can we do?</h1><hr /> <p align="center">Here at The Castle, when it comes to web design we can do almost anything! We can do Ecommerce, mobile websites, personal websites, and much more! Here, no matter what kind of business you have, you can get a site for it.</p> </div> 

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

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