Javascript classList.toggle没有删除类

时间:2017-12-03 17:29:51

标签: javascript tabs accordion responsive

我试图切换课程"打开"这样我的手风琴会在点击同一个按钮时关闭。点击另一个按钮时它们会很好,但是当点击它自己时则不然。这需要在Javascript而不是jQuery中完成。请在移动设备上查看问题所在:

这是我的JSFiddle链接:https://jsfiddle.net/pwouns7o/1/

这是我的Javascript代码:

var accordion = document.getElementsByClassName("accordion");
var panel = document.getElementsByClassName("panel");

for(var i = 0; i < accordion.length; i++){
  accordion[i].addEventListener('click', function(){
    removeOpen();
    // add active class to clicked element and open class to next slibling
    this.nextElementSibling.classList.toggle('open');
    this.classList.toggle('active');
  });
};

function removeOpen (){
 // remove open class for all elements
    for(var i = 0; i < panel.length; i++) {
       panel[i].classList.remove('open');
    }
};

我不确定我做错了什么?

0 个答案:

没有答案