gpt4 book ai didi

html - 在初始状态下单击按钮两次时显示的 Div

转载 作者:行者123 更新时间:2023-12-02 02:42:38 25 4
gpt4 key购买 nike

在最初始的状态下,我需要点击“阅读更多”按钮两次才能显示下面的内容。奇怪 - 我该如何解决这个问题?我只想单击“阅读更多”按钮一次以显示其下方的内容。

function myFunction() {
var x = document.getElementById("myDIV");
var btnText = document.getElementById("myBtn");
if (x.style.display === "none") {
x.style.display = "block";
btnText.innerHTML = "Read less";
} else {
x.style.display = "none";
btnText.innerHTML = "Read More";
}
}
   #myDIV {
display: none;
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
}
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
<div id="myDIV">
This is my DIV element.
</div>
<button onclick="myFunction()" id="myBtn">Read More</button>
<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

最佳答案

您应该检查 myDivdisplay 样式是否为空。

function myFunction() {
var x = document.getElementById("myDIV");
var btnText = document.getElementById("myBtn");

if (x.style.display === "none" || x.style.display === "") { // notice this line
x.style.display = "block";
btnText.innerHTML = "Read less";
} else {
x.style.display = "none";
btnText.innerHTML = "Read More";
}
}
#myDIV {
display: none;
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
}
<p>
Click the "Try it" button to toggle between hiding and showing the DIV element:
</p>
<div id="myDIV">
This is my DIV element.
</div>

<button onclick="myFunction()" id="myBtn">Read More</button>

<p>
<b>Note:</b> The element will not take up any space when the display property set to "none".
</p>

关于html - 在初始状态下单击按钮两次时显示的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63322950/

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