简体   繁体   English

在JavaScript中显示/隐藏表单

[英]Show/Hide a form in JavaScript

I'm trying to hide and show "form1". 我试图隐藏并显示“form1”。 But even simply hiding doesn't work. 但即使只是隐藏也行不通。
Where is the mistake? 哪里出错了?

<body>

    <script type="text/javascript">
        document.getElementById("F1").style.visibility = "hidden";
    </script>

    <form id="F1" name="form1">
        <p class="style14">blah-blah
            <input type="text" size="1" name="rd"> blah
        </p>
    </form>

</body>

Firstly you need to make sure your script tag is at the bottom of the body or use the DOMContentLoaded event 首先,您需要确保script标记位于body的底部或使用DOMContentLoaded事件

like so 像这样

document.addEventListener('DOMContentLoaded', function(){
   var form = document.getElementById('F1');

   form.style.visibility="hidden"; 
   // OR
   form.style.display = 'none';
});

Your F1 needs to be a string, right now you're referring to a undefined variable. 你的F1需要是一个字符串,现在你指的是一个未定义的变量。

And I also recommend using display instead of visibility 我还建议使用display而不是visibility

@update to comment. @update发表评论。

The opposites of them are 它们的对立面是

visibility: visible;

AND

display: block; // Or whatever 

This line is wrong 这条线错了

document.getElementById(thediv).style.visibility="hidden";

What is "thediv" you should use : 什么是你应该使用的“thediv”:

document.getElementById("F1").style.visibility="hidden";

F1 should be wrapped in quotes. F1应该用引号括起来。 You also might need to encompass your code within the onload function 您还可能需要在onload函数中包含您的代码

window.onload = function(){
    document.getElementById("F1").style.visibility = "hidden";
}

尝试document.getElementById("F1").style.visibility=hidden;

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

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