【发布时间】:2022-01-03 07:29:07
【问题描述】:
在这里我试图隐藏块,但我第一次需要在每次刷新页面时双击按钮。我在下面附上了我的代码以供参考。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#myDIV {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
display: none;
}
</style>
</head>
<body>
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
<button onclick="myFunction()">Try it</button>
<div id="myDIV">
This is my DIV element.
</div>
<p><b>Note:</b> The element will not take up any space when the display property set to "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>
为什么会这样,有人可以告诉我我在哪里犯错了吗?
【问题讨论】:
-
为什么不直接使用
<details><summary>?这样你根本不需要任何 JS。
标签: javascript html css