以下是代码
<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">
此代码不起作用,任何人都可以帮助我!!
答案 0 :(得分:4)
您应该使用:
div.style.display = div.style.display == "none" ? "block" : "none";
答案 1 :(得分:4)
关闭,但您没有将显示值分配给display属性。试试这个:
div.style.display = (div.style.display == "none") ? "block" : "none";
答案 2 :(得分:0)
HTML
<input type="button" onclick="show()" value="Click Me" id="myBtn"/>
<div id="mydiv">This is Div</div>
<强> CSS:强>
#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';
} };