嵌套的手风琴儿童在父母关闭后没有关闭

时间:2016-09-23 07:55:59

标签: javascript

我正在研究嵌套的accordion.i希望当我关闭我的父母手风琴时,打开儿童手风琴会自动关闭。 这是我的javaScript函数:

    var accc = document.getElementsByClassName("accordion");
    var panell = document.getElementsByClassName('panel1');
    var acc = document.getElementsByClassName("accordion_inner");
    var panel = document.getElementsByClassName('panel2');

    for (var i = 0; i < accc.length; i++) {
        accc[i].onclick = function () {
           var setClasses = !this.classList.contains('active');
            setClass(accc, 'active', 'remove');
          setClass(panell, 'show', 'remove');

           if (setClasses) {
                this.classList.toggle("active");
                this.nextElementSibling.classList.toggle("show");
           }
       }
    }
    for (var i = 0; i < acc.length; i++) {
        acc[i].onclick = function () {
            var setClasses = !this.classList.contains('active');
            setClass(acc, 'active', 'remove');
            setClass(panel, 'show', 'remove');

            if (setClasses) {
                this.classList.toggle("active");
                this.nextElementSibling.classList.toggle("show");
            }
        }
    }function setClass(els, className, fnName) {
      for (var i = 0; i < els.length; i++) {
            els[i].classList[fnName](className);
        }
   }function setClass(els, className, fnName) {
       for (var i = 0; i < els.length; i++) {
           els[i].classList[fnName](className);
       }
   }

0 个答案:

没有答案