【发布时间】:2018-04-02 17:54:32
【问题描述】:
我正在尝试调整 w3schools.com 的功能,通过在 display:none 和 display:block 之间切换来切换 div 的可见性。
该div的显示属性是通过css定义的。如果显示最初设置为“阻止”,则一切正常,但如果最初设置为“无”,则不会。
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
#myDIV {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
display : block;
}
<p>Click the button to toggle between hiding and showing the DIV element:</p>
<button onclick="myFunction()">Click</button>
<div id="myDIV">
This is my DIV element.
</div>
请看这个codepen example。 如果您将 css 更改为显示:无;您需要在 div 切换为阻止之前单击按钮两次(!)。为什么脚本无法识别初始的“无”?
【问题讨论】:
标签: javascript html css