简体   繁体   English

为什么我的文字会短暂显示,然后突然变得不可见?

[英]Why does my text briefly show up and then suddenly becomes invisible?

I have a button that, when clicked, reveals hidden text by changing its CSS display property to "block".我有一个按钮,单击该按钮时,通过将其 CSS 显示属性更改为“块”来显示隐藏的文本。 However, when I click the button, the text briefly shows up and then immediately disappear afterwards.但是,当我单击按钮时,文本会短暂显示,然后立即消失。 How do I fix this?我该如何解决?

 const contactPopUp = document.getElementById("contactButton"); const exitButton = document.getElementById("exit"); function showPopUp() { document.getElementById("contacts").style.cssText = "display: block; position: absolute; top: 30%; right: 40%; background-color: rgb(0, 0, 119); font - family: verdana, sans - serif; color: white; padding: 30 px;"; } function closePopUp() { document.getElementById("contacts").style.display = "none"; } contactPopUp.addEventListener("click", showPopUp); exitButton.addEventListener("click", closePopUp);
 #contacts { display: none; position: absolute; top: 30%; right: 40%; background-color: rgb(0, 0, 119); font-family: verdana, sans-serif; color: white; padding: 30px; } #contact_list { display: block; font-family: verdana, sans-serif; color: white; }
 <div id="header_container"> <ul> <li id="homepage_name">text</li> <li><a href="">Home</a></li> <li><a href="">About us</a></li> <li><a id="contactButton" href="">Contacts</a></li> </ul> </div> <div id="contacts"> You may contact us at: <button id="exit">X</button> <ul> <li id="contact_list">Insert Contacts Here.</li> <li id="contact_list">Insert Contacts Here.</li> <li id="contact_list">Insert Contacts Here.</li> </ul> </div>

The problem is that href="" reloads the page.问题是href=""重新加载页面。 You may want to use href="#" or make the button an actual button.您可能想要使用href="#"或使按钮成为实际按钮。

 const contactPopUp = document.getElementById("contactButton"); const exitButton = document.getElementById("exit"); function showPopUp() { document.getElementById("contacts").style.cssText = "display: block; position: absolute; top: 30%; right: 40%; background-color: rgb(0, 0, 119); font - family: verdana, sans - serif; color: white; padding: 30 px;"; } function closePopUp() { document.getElementById("contacts").style.display = "none"; } contactPopUp.addEventListener("click", showPopUp); exitButton.addEventListener("click", closePopUp);
 #contacts { display: none; position: absolute; top: 30%; right: 40%; background-color: rgb(0, 0, 119); font-family: verdana, sans-serif; color: white; padding: 30px; } #contact_list { display: block; font-family: verdana, sans-serif; color: white; }
 <div id="header_container"> <ul> <li id="homepage_name">text</li> <li><a href="">Home</a></li> <li><a href="">About us</a></li> <li> <a id="contactButton" href="#">Contacts</a> <!-- <button id="contactButton">Contacts</a> --> </li> </ul> <div id="contacts"> You may contact us at: <button id="exit">X</button> <ul> <li id="contact_list">Insert Contacts Here.</li> <li id="contact_list">Insert Contacts Here.</li> <li id="contact_list">Insert Contacts Here.</li> </ul>

This happens because when you click on the Contacts button, the page reloads.发生这种情况是因为当您单击“联系人”按钮时,页面会重新加载。 And this happens because the A tag surrounding your button has the empty href="" attribute, which tells the site to reload the page when the button is clicked, you can fix this by adding href="#".发生这种情况是因为您的按钮周围的 A 标记具有空的 href="" 属性,该属性告诉网站在单击按钮时重新加载页面,您可以通过添加 href="#" 来解决此问题。 Also rewrote your code a bit, besides the div tag with id="header_container" didn't have a closing tag.除了 id="header_container" 的 div 标签没有结束标签之外,还稍微重写了您的代码。

 #contacts {
   display: none;
 }
 #contacts._active {
   display: block; 
   position: absolute; 
   top: 30%; 
   right: 40%; 
   background-color: rgb(0, 0, 119); 
   font-family: verdana, sans-serif; 
   color: white; 
   padding: 30px;
 }
 
 #contact_list {
   display: block;
   font-family: verdana, sans-serif;
   color: white;
 }
</style>
 
<div id="header_container">
 <ul>
   <li id="homepage_name">text</li>
   <li><a href="">Home</a></li>
   <li><a href="">About us</a></li>
   <li><a id="contactButton" href="#">Contacts</a></li>
 </ul>
 <div id="contacts">
   You may contact us at: <button id="exit">X</button>
   <ul>
     <li id="contact_list">Insert Contacts Here.</li>
     <li id="contact_list">Insert Contacts Here.</li>
     <li id="contact_list">Insert Contacts Here.</li>
   </ul>
 </div>
<script>
 const contactPopUp = document.getElementById("contactButton");
 const exitButton = document.getElementById("exit");
   
 contactPopUp.addEventListener("click", function() {
   document.getElementById("contacts").classList.add("_active");
 });
 exitButton.addEventListener("click", function() {
   document.getElementById("contacts").classList.remove("_active");
 });
</script>

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

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