gpt4 book ai didi

javascript - 如何在javascript中显示/隐藏一个div?

转载 作者:行者123 更新时间:2023-11-30 12:17:07 25 4
gpt4 key购买 nike

当我单击按钮时,我希望显示 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com