<html>
<head>
<style>
#myDIV {
width: 500px;
height: 500px;
background-color: lightblue;
}
</style>
</head>
<body>
<p>单击“试一试”按钮在隐藏和显示 DIV 元素之间切换:</p>
<button onclick="myFunction()">试一试</button>
<div id="myDIV">
这是我的 DIV 元素。
</div>
<p><b>注意:</b>当display属性设置为“none”时,该元素不会占用任何空间。</p>
<script>
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
</body>
</html>