<分区>
为什么当我尝试隐藏一个 div 时
运行它不会消失吗?据我了解,您可以使用 document.getElementById('something').style.display = "none";
隐藏某些内容。然而,当我尝试在显示和隐藏它之间进行操作时,它不起作用。
function someComplicatedFunction(ms) {
// This isn't the exact function I am using,
// but it has a similar form and the same problem
let start = new Date().getTime();
let end = start;
while (end < start + ms) {
end = new Date().getTime();
}
}
function hideTheOtherDiv() {
document.getElementById('div1').style.display = "none";
document.getElementById('div2').style.display = "initial";
console.log("Start");
someComplicatedFunction(3000);
console.log("end");
document.getElementById('div1').style.display = "initial";
document.getElementById('div2').style.display = "none";
}
document.getElementById('div2').style.display = "none";
<!DOCTYPE html>
<html>
<body>
<div id="div1">One</div>
<div id="div2">Two</div>
<button onclick="hideTheOtherDiv()">Switch</button>
</body>
<script type="text/javascript">
</script>
</html>