[英]How do i make a <div> section invisible when a button is clicked (JS)?
如何使用 JS 隱藏<div>
部分和
<div class="alertB1">
<div class="sticky">
<h1> This site is still being built</h1>
<p> Please remember this site is still being built. Click <a href="form1.html">here to report a bug </a>
</div>
</div>
<style>
.sticky {
position: -webkit-sticky;
position: sticky;
}
.alertB1 {
width: 100%;
height: 125px;
background: lightgreen;
}
</style>
所以我希望包含一個按鈕:
<span id="a33"><button>×</button></span>
</div>
</div>
如何讓<span>
隱藏<div>
標簽? 謝謝,環形游戲
使用onclick
屬性;
更多信息: DOM onevent 處理程序
.sticky { position: -webkit-sticky; position: sticky; }.alertB1 { width: 100%; height: 125px; background: lightgreen; }
<div class="alertB1"> <div class="sticky"> <h1> This site is still being built</h1> <p> Please remember this site is still being built. Click <a href="form1.html">here to report a bug </a></p> <span onclick="document.getElementsByClassName('alertB1')[0].style.display = 'none'" id="a33"><button>×</button></span> </div> </div>
function onToggle() { const ele = document.querySelector(".toggle-div"); ele.classList.toggle("toggle"); }
body { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; }.button { position: relative; background-color: #4caf50; border: none; font-size: 28px; color: #ffffff; padding: 20px; width: 200px; text-align: center; -webkit-transition-duration: 0.4s; /* Safari */ transition-duration: 0.4s; text-decoration: none; overflow: hidden; cursor: pointer; }.toggle-div.toggle { visibility: hidden; }
<body> <button onclick="onToggle()" class="button">Click here to toggle</button> <div class="toggle-div"> <h1>Toggle Div on click</h1> </div> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.