JavaScript& CSS手风琴列表

时间:2017-04-05 19:39:55

标签: javascript html css accordion

我使用CSS和JavaScript获得了手风琴列表的代码。当我点击标题时,它会显示隐藏的内容。如果我点击相同的标题,我将如何做到这一点,它会再次隐藏内容?任何事都有帮助,欢呼。



(function () {
  var accordions, i;
  
  // Make sure the browser supports what we are about to do.
  if (!document.querySelectorAll || !document.body.classList) return;
  
  // Using a function helps isolate each accordion from the others
  function makeAccordion(accordion) {
    var targets, currentTarget, i;
    
    targets = accordion.querySelectorAll('.accordion > * >h1 ');
    for(i = 0; i < targets.length; i++) {
      targets[i].addEventListener('click', function () {
        if (currentTarget)
          currentTarget.classList.remove('expanded');
        
        currentTarget = this.parentNode;
        currentTarget.classList.add('expanded');
      }, false);
    }

    accordion.classList.add('js');
  }

  // Find all the accordions to enable
  accordions = document.querySelectorAll('.accordion');
  
  // Array functions don't apply well to NodeLists
  for(i = 0; i < accordions.length; i++) {
    makeAccordion(accordions[i]);
  }
  
})();
&#13;
<style>
.accordion.js > * {
  overflow: hidden;
}

.accordion.js > *:not(.expanded) > *:not(h1) {
  max-height: 0;
  margin-top: 0;
  margin-bottom: 0;
  opacity: 0;
  visibility: hidden;
}

.accordion.js > .expanded > *:not(h1) {
  max-height: 10em;
  opacity: 1;
  visibility: visible;
}

.accordion.js > * > h1 {
  cursor: pointer;
  visibility: visible;
}

.accordion.js > * > *:not(h1) {
  transition: max-height 0.7s,
    visibility 1s,
    margin 1s,
    opacity 1s;
}

.sections {
color:#5E5E5E;
text-align:center;
width:90%;
border-style:solid;
border-width:1px;
border-color:#D1D1D1;
padding: 0 .5em;
background-color:#FFFFFF;
border-radius:3px;
}
</style>
&#13;
<section class="accordion">
  <section class="sections">
    <h1>A</h1>
    <p>All content for A.</p>
  </section>
  <br style="line-height:5px"/>
  <section class="sections">
    <h1>B</h1>
    <p>All content for B</p>
  </section>
  <br style="line-height:5px"/>
  <section class="sections">
    <h1>C</h1>
    <p>All content for C<p>
  </section>
</section>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这应该有用。

我所做的只是添加一个条件来检查手风琴类是否退出目标父级,如果确实如此则将其删除。否则其他一切都是一样的。

.accordion.js > * {
  overflow: hidden;
}

.accordion.js > *:not(.expanded) > *:not(h1) {
  max-height: 0;
  margin-top: 0;
  margin-bottom: 0;
  opacity: 0;
  visibility: hidden;
}

.accordion.js > .expanded > *:not(h1) {
  max-height: 10em;
  opacity: 1;
  visibility: visible;
}

.accordion.js > * > h1 {
  cursor: pointer;
  visibility: visible;
}

.accordion.js > * > *:not(h1) {
  transition: max-height 0.7s,
    visibility 1s,
    margin 1s,
    opacity 1s;
}

.sections {
color:#5E5E5E;
text-align:center;
width:90%;
border-style:solid;
border-width:1px;
border-color:#D1D1D1;
padding: 0 .5em;
background-color:#FFFFFF;
border-radius:3px;
}
<section class="accordion">
  <section class="sections">
    <h1>A</h1>
    <p>All content for A.</p>
  </section>
  <br style="line-height:5px"/>
  <section class="sections">
    <h1>B</h1>
    <p>All content for B</p>
  </section>
  <br style="line-height:5px"/>
  <section class="sections">
    <h1>C</h1>
    <p>All content for C<p>
  </section>
</section>
Exception in thread "main" java.lang.NoClassDefFoundError: no/uib/cipr/matrix/Vector
    at assignment2.face.tryLDA(face.java:141)
    at assignment2.Assignment2.main(Assignment2.java:106)
Caused by: java.lang.ClassNotFoundException: no.uib.cipr.matrix.Vector
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)