I made a button similar to "Read More" and when I press it, a text expands down. When the text expands, the button's background changes, and when I press the button again to close the text (e.g. "Read Less"), the button's background returns to its original state. How to implement this function
function myFunction() { var dots = document.getElementById("dots"); var moreText = document.getElementById("more"); var btnText = document.getElementsByClassName("skillBtn"); if (dots.style.display === "none") { dots.style.display = "inline"; btnText.innerHTML = "閱讀更多"; moreText.style.display = "none"; } else { dots.style.display = "none"; btnText.innerHTML = "閱讀更少"; moreText.style.display = "inline"; } }
#more {display: none;}
<div align="center"><h1 id="skillsid" style="margin-top:10px ;"> 編程語言 <span id="dots"></span> </h1><span id="more"> 這里寫了一些內(nèi)容 <div align="center"><a href="#skillsid"><button class="skillBtn" onclick="myFunction()" ></button></a></div>
What you are trying to do is called accordion. There is no need to write in JS, because HTML already has <details>
and <summary>
to implement this function:
#more { display: none; }
<details> <summary>編程語言</summary> 這里寫了一些內(nèi)容 </details>