![](/img/trans.png)
[英]how to add a css class when button clicked and and also remove that with another button click
[英]How to remove class from button when another button is clicked using pure Javascript
我很難使它正常工作。
我有一個響應式的標簽/手風琴。 但是,當單擊一個按鈕時,將應用“活動”類,但在單擊另一按鈕時,“活動”類仍保留在前一個按鈕上。 我必須使用Javascript而非jQuery來實現。
我收到錯誤“未捕獲的TypeError:無法讀取HTMLButtonElement上未定義的屬性'remove'”。
這是我目前的代碼:
var accordion = document.getElementsByClassName("accordion");
var activeAccordion = document.getElementsByTagName("button");
var i;
for (i = 0; i < accordion.length; i++) {
accordion[i].addEventListener("click", function(){
this.classList.toggle("active");
activeAccordion.classList.remove("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
})
}
取代這個
activeAccordion.classList.remove("active");
通過
activeAccordion.classList.removeClass("active");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.