简体   繁体   English

使用JavaScript切换显示div。 开始隐藏,单击以显示

[英]toggle reveal div with javascript. Start hidden, click to reveal

I would like to toggle whether a div is visible using a button to hide/reveal the div on click. 我想使用单击按钮隐藏/显示div来切换div是否可见。

I have code which will do this, but the div is visible on page load by default. 我有执行此操作的代码,但是默认情况下,div在页面加载时可见。 I'd like the div to start hidden and only be revealed when the viewer clicks. 我希望div开始隐藏,并且仅在查看者单击时才显示。

Here is the code I am using currently. 这是我当前正在使用的代码。 Is there a way to change this so the div starts hidden? 有没有办法改变这一点,使股利开始隐藏?

<script>
function myFunction() {
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') {
        x.style.display = 'block';
    } else {
        x.style.display = 'none';
    }
}
</script>

<button onclick="myFunction()">Enquire Now &gt;</button>
<div id="myDIV">
    <form action="/post.php" method="post">
        <p>Enter your contact details</p>
        <p>
        Name: <input type="text" name="name"><br>
        Tel: <input type="tel" name="tel"><br>
        Email: <input type="email" name="email" value="Email Address"></p>

        <p><input type="submit"></p>
    </form>
</div>

Change 更改

<div id="myDIV">

to

<div id="myDIV" style="display: none;">

您可以使用style属性:

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

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

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