当我单击按钮时,我希望显示 div,当我再次单击按钮时,我希望它消失。我究竟做错了什么?
<button type="button" onclick="myFunction()">Click Me!</button>
<div id="Dglow" class="Dglow">
Glow
</div>
<script>
function myFunction() {
var e = document.getElementById("Dglow").style.display;
if (e == "none")
e = "block";
else {
e = "none";
}
}
最佳答案
您应该比较并更改元素的 display
属性:
function myFunction() {
var e = document.getElementById("Dglow").style;
if (e.display == "none") {
e.display = "block";
} else {
e.display = "none";
}
}
<button type="button" onclick="myFunction()">Click Me!</button>
<div id="Dglow" class="Dglow">Glow</div>
关于javascript - 如何在javascript中显示/隐藏一个div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32156784/