gpt4 book ai didi

javascript - 使用 display 属性操作元素

转载 作者:太空宇宙 更新时间:2023-11-04 03:04:21 27 4
gpt4 key购买 nike

<!DOCTYPE html>
<html>

<head>
<style>
#myDIV {
width: 500px;
height: 500px;
background-color: lightblue;
display: none;
}

#second {
width: 500px;
height: 500px;
background-color: lightblue;
display: none;
}
</style>
</head>

<body>

<button onclick="myFunction()">Try it</button>
<button onclick="myFunction2()">Try this now</button>
<div id="myDIV">
This is my DIV element.
</div>
<div id="second">
This is my DIV2 element.
</div>

<script>
function myFunction() {
document.getElementById("myDIV").style.display = "block";
}
</script>

<script>
function myFunction2() {
document.getElementById("second").style.display = "block";
}
</script>
</body>

</html>

所以我有这段代码,可以在单击按钮时显示一个 div 元素。但是我希望一旦我点击“第二个”按钮,我希望“myDiv”消失并且“第二个”取代它的位置(确切位置),反之亦然。

有什么帮助吗?

最佳答案

<script>
function myFunction() {
document.getElementById("myDIV").style.display = "block";
document.getElementById("second").style.display = "none";
}
</script>

<script>
function myFunction2() {
document.getElementById("second").style.display = "block";
document.getElementById("myDIV").style.display = "none";
}
</script>

关于javascript - 使用 display 属性操作元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30787343/

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