如何使用javascript在单击按钮时显示或隐藏div

如何使用javascript在单击按钮时显示或隐藏div,javascript,html,Javascript,Html,以下是代码 <style type="text/css"> #mydiv{ margin:0 auto;padding:0;width:250px;height:100px; border:1px solid threedshadow;display:none;} </style> <script type="text/javascript"> function show() { var div=document.getElementById('mydiv'

以下是代码

<style type="text/css">
#mydiv{ margin:0 auto;padding:0;width:250px;height:100px; border:1px solid threedshadow;display:none;}
</style>

<script type="text/javascript">
function show()
{
var div=document.getElementById('mydiv');
div.style.display == "none" ? "block" : "none";
}
</script>



<input type="button" onclick="show()" value="show"/>
<div id="mydiv">
这个代码不起作用,有人能帮我吗

您应该使用:

div.style.display = div.style.display == "none" ? "block" : "none";

关闭,但您没有将显示值指定给显示属性。试试这个:

div.style.display = (div.style.display == "none") ? "block" : "none";
HTML

脚本:


因为这个代码不起作用,有人能帮我吗!!这不是一个问题描述@Dropout@sidhewsar如果你还没有,一定要关闭你的潜水舱:@PeeHaa well。。是 啊这是真正的HTML这是Div CSS:@Peehaa当我没有意识到这段代码不起作用时,任何人都能帮我!!它会显示一条错误消息,看起来你的帖子大部分都是代码;请添加更多详细信息。这就是为什么我要写那句话
<input type="button" onclick="show()" value="Click Me" id="myBtn"/> 
<div id="mydiv">This is Div</div>
#mydiv{ 
 margin:0 
 auto;padding:0;
 width:250px;
 height:100px;
 border:1px solid threedshadow;
 display:none;}
var showBtn = document.getElementById('myBtn');

showBtn.onclick = function() {
    var showme = document.getElementById('mydiv');
    if (showme.style.display !== 'none') {
        showme.style.display = 'none';
    }
    else {
        showme.style.display = 'block';
    } };