Click to expand shall result in collapsing other opened items?

不想你离开。 提交于 2019-12-08 11:14:44

问题


when i click on the menu item, i want just that item to expand and rest shall collapse.

example: i click on item 1 to expand it, then i click on item 2. So, in this case item 1 shall collapse itself.

html

<ul class="footernav">
  <li>
    <a href="javascript:;" class="topToggle">Item 1</a>
    <div class="typeToggle typeCont">
    Link area one
    </div>
  </li>
  <li>
    <a href="javascript:;" class="topToggle">Item 2</a>
    <div class="typeToggle filterCont">Link area two</div>
  </li>
</ul>

Jquery

$(document).ready(function(){ 
  $(".topToggle").click( function () {  
   $(this).parent().children('.typeToggle').slideToggle();
  });
});

FIDDLE


回答1:


You can slideUp() all .typeToggles before you slideToggle() the specific one.

$(".topToggle").click(function () {
        $(".typeToggle").slideUp();
        $(this).parent().children('.typeToggle').slideToggle();
    });

This will result in a UP/DOWN motion if you click the already expanded item but you could put in some checks to stop that if you wish.

Example fiddle


Edit:

This is what is known as an Accordion and many examples and plugins can be found on the web. jQuery UI provides this functionality you can see it here; if you don't want to recreate the functionality it is usually trivial to add a plugin and have it perform this for you.



来源:https://stackoverflow.com/questions/23270323/click-to-expand-shall-result-in-collapsing-other-opened-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!