[英]how can I add/remove class on children element
See the code below:请看下面的代码:
.nk-section-icons {
height: 30px;
min-width: 30px;
width: 30px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-radius: 6px;
position: relative;
background: yellow;
margin-bottom: 5px;
}
.nk-section-icons .nk-sec-container {
r
}
.nk-sec-container {
height: 100%;
width: 150px;
background: green;
position: absolute;
left: 50px;
margin: bottom: 10px;
}
.nk-sec-container.addClass {
background: red;
}
<div class="nk-section-l-icons">
<div class="nk-section-icons fav" data-title="Favorites">
<div class="nk-sec-container nk-sec-fav-c">
</div>
</div>
<div class="nk-section-icons recent" data-title="Recent">
<div class="nk-sec-container nk-sec-recent-c">
</div>
</div>
<div class="nk-section-icons notifs" data-title="Notifications">
<div class="nk-sec-container nk-sec-notif-c">
</div>
</div>
</div>
const hrdBtn = document.querySelectorAll(".nk-section-icons") hrdBtn.forEach((hrdBtns)=> { hrdBtns.addEventListener("click", (container)=> { const btnContainer = container.currentTarget.children[0] btnContainer.classList.toggle("addClass") }) })
.nk-section-icons { height: 30px; min-width: 30px; width: 30px; display: flex; flex-direction: column; align-items: center; justify-content: center; border-radius: 6px; position: relative; background: yellow; margin-bottom: 5px; }.nk-section-icons.nk-sec-container { r }.nk-sec-container { height: 100%; width: 150px; background: green; position: absolute; left: 50px; margin: bottom: 10px; }.nk-sec-container.addClass { background: red; }
<div class="nk-section-l-icons"> <div class="nk-section-icons fav" data-title="Favorites"> <div class="nk-sec-container nk-sec-fav-c addClass"> </div> </div> <div class="nk-section-icons recent" data-title="Recent"> <div class="nk-sec-container nk-sec-recent-c"> </div> </div> <div class="nk-section-icons notifs" data-title="Notifications"> <div class="nk-sec-container nk-sec-notif-c"> </div> </div> </div>
Can anyone help me, Currently my javascript is working it's adding and removing the class addClass
?谁能帮助我,目前我的 javascript 正在添加和删除 class
addClass
吗? But I need to click the button again to remove the addClass
.但我需要再次单击该按钮以删除
addClass
。 My goal is I want my code if I click the 2nd button the addClass
in my children's first div will remove and it will be transferred to the 2nd button children's div.我的目标是我想要我的代码,如果我点击第二个按钮,我的孩子的第一个 div 中的
addClass
将被删除,它将被转移到第二个按钮的孩子的 div。
const hrdBtn = document.querySelectorAll(".nk-section-icons") hrdBtn.forEach((hrdBtns)=> { hrdBtns.addEventListener("click", (container)=> { // new code: remove active state on all others hrdBtn.forEach(button => button.children[0].classList.remove('addClass')) const btnContainer = container.currentTarget.children[0] btnContainer.classList.toggle("addClass") }) })
.nk-section-icons { height: 30px; min-width: 30px; width: 30px; display: flex; flex-direction: column; align-items: center; justify-content: center; border-radius: 6px; position: relative; background: yellow; margin-bottom: 5px; }.nk-section-icons.nk-sec-container { r }.nk-sec-container { height: 100%; width: 150px; background: green; position: absolute; left: 50px; margin: bottom: 10px; }.nk-sec-container.addClass { background: red; }
<div class="nk-section-l-icons"> <div class="nk-section-icons fav" data-title="Favorites"> <div class="nk-sec-container nk-sec-fav-c addClass"> </div> </div> <div class="nk-section-icons recent" data-title="Recent"> <div class="nk-sec-container nk-sec-recent-c"> </div> </div> <div class="nk-section-icons notifs" data-title="Notifications"> <div class="nk-sec-container nk-sec-notif-c"> </div> </div> </div>
Because you will only have one element with addClass
, you can select the element with document.querySelector(".addClass")
and remove the class.因为您将只有一个带有
addClass
的元素,您可以使用document.querySelector(".addClass")
select 元素并删除 class。
const hrdBtn = document.querySelectorAll(".nk-section-icons") hrdBtn.forEach((hrdBtns)=> { hrdBtns.addEventListener("click", (container)=> { document.querySelector(".addClass").classList.remove("addClass"); const btnContainer = container.currentTarget.children[0]; btnContainer.classList.toggle("addClass"); }) })
.nk-section-icons { height: 30px; min-width: 30px; width: 30px; display: flex; flex-direction: column; align-items: center; justify-content: center; border-radius: 6px; position: relative; background: yellow; margin-bottom: 5px; } /*.nk-section-icons.nk-sec-container { r }*/.nk-sec-container { height: 100%; width: 150px; background: green; position: absolute; left: 50px; margin: bottom: 10px; }.nk-sec-container.addClass { background: red; }
<div class="nk-section-l-icons"> <div class="nk-section-icons fav" data-title="Favorites"> <div class="nk-sec-container nk-sec-fav-c addClass"></div> </div> <div class="nk-section-icons recent" data-title="Recent"> <div class="nk-sec-container nk-sec-recent-c"></div> </div> <div class="nk-section-icons notifs" data-title="Notifications"> <div class="nk-sec-container nk-sec-notif-c"></div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.