简体   繁体   English

将CSS“display:none;”更改为显示

[英]Change CSS “display: none;” to shown

I want an element on my website hidden when the website loads, but when a user submits a form, then it changes to show. 我想在网站加载时隐藏我网站上的元素,但是当用户提交表单时,它会更改为显示。 I have tried for hours now, can't get it to work. 我已经试了几个小时了,不能让它上班。

Code: 码:

HTML HTML

<form onsubmit="show();">
<div id="mymessage" class="hidden">Message was sent.</div>

CSS CSS

.hidden {
  display: none;
}

JavaScript JavaScript的

function show () {
  document.getElementById("mymessage").style.display = 'block';
}

Disclaimer: I only included relevant code. 免责声明:我只包含相关代码。

Hope somebody is able to help me/spot the error! 希望有人能够帮助我/发现错误!

You have to return false to prevent the browser from refreshing the page so that you can hide the element on submit: 您必须返回false以防止浏览器刷新页面,以便您可以在提交时隐藏元素:

 function show () { document.getElementById("mymessage").style.display = 'block'; return false; } 
 .hidden { display: none; } 
 <form onsubmit="return show();"> <button>enviar</button> </form> <div id="mymessage" class="hidden">Message was sent.</div> 

I would remove the hidden class: 我会删除hidden类:

function show () {
  document.getElementById("mymessage").classList.remove("hidden");
}

( classList has very broad support, and can be polyfilled on obsolete browsers if necessary.) classList具有非常广泛的支持,如果需要,可以在过时的浏览器上进行classList 。)

Live Example (using a button click instead of a form submit): 实例(使用按钮单击而不是表单提交):

 function show () { document.getElementById("mymessage").classList.remove("hidden"); return false; } 
 .hidden { display: none; } 
 <form> <div id="mymessage" class="hidden">Message was sent.</div> <input type="button" onclick="show();" value="Click Me"> 

我认为这是一个解决方案

document.getElementById("mymessage").classList.remove("hidden");

What about if you use jQuery instead? 如果你使用jQuery呢?

</script>
    <script>
    $(document).ready(function(){
        $("form").submit(function(){
            jQuery('#mymessage').show();
        });
    });
    </script>

Remember to add the library above the code. 请记住在代码上方添加库。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">

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

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