如何在单击另一个手风琴时自动关闭Javascript手风琴

时间:2017-06-14 21:51:09

标签: javascript

我在一个设计的网站上有几支手风琴。基本上,手风琴显示颜色的缩略图版本,您可以单击以更改某个元素的颜色。但是,如果您单击一个手风琴,然后单击另一个手风琴,颜色会重叠。我试图这样做,当点击另一个手风琴时,它会自动关闭之前打开的手风琴。这是代码:

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    /* Toggle between adding and removing the "active" class,
    to highlight the button that controls the panel */
    this.classList.toggle("active");

    /* Toggle between hiding and showing the active panel */
    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
      $(".accordion, .big-accordion").accordion({
        event: "mouseover"
      });
      $(".accordion .ui-accordion-header a").css({
        padding: "1px 1px 1px 30px"
      });
    }
  }
}

有什么想法吗?

0 个答案:

没有答案