简体   繁体   English

加载时隐藏 div 并在单击按钮后显示

[英]hiding div on load and displaying after button click

im currently learning css/js/php我目前正在学习 css/js/php
everytime i click the button it shows the div by split second and the hides it again immediately每次我单击按钮时,它都会瞬间显示 div,然后立即再次隐藏
can someone help有人可以帮忙吗
this is the part of the code that im talking about这是我正在谈论的代码的一部分

 function showError() { document.getElementById('error').style.display = "block"; }
 <form> <input type="password"> <div id="error" style="display: none;">Invalid Password</div> <br/> <button type="submit" onclick="showError()">LOG IN</button> </form>

 <form> <input type="password"> <div id="error" style="display: none;">Invalid Password </div> <button type="button" onclick="return showError()">LOG IN</button> </form> <script> function showError(){ document.getElementById('error').style.display="block"; } </script>

There are couple of mistakes first the span tag should be removed since its not closed properly and div should be closed correctly and button should be outside the div tag and mostly importantly button type should not be submit it should be button if its submit the form will be submitted and page will reload as a result it will show the div again有几个错误首先应该删除span标签,因为它没有正确关闭,div应该正确关闭,按钮应该在div标签之外,最重要的是按钮类型不应该提交它应该是按钮,如果它提交表单将被提交,页面将重新加载,结果它将再次显示 div

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

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