使有序列表可折叠 [英] Make ordered list collapsible

查看:105
本文介绍了使有序列表可折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有序列表,我希望在用户单击链接时默认情况下使其可折叠,并且可以展开.

I have an ordered list which i want to make collapsible by default and expandable when user click on the link.

https://jsfiddle.net/rkmv3rn3/17/

如何使它正常工作

使用以下脚本,它会折叠所有父项,然后无法正确打开它们.

With following script it collapses all Parent item then fails to open them properly.

$(window).load(function() {
  prepareList();
});

function prepareList() {
  $('#expList').find('li:has(ol)')
  .click(function(event) {
    if (this == event.target) {
      $(this).toggleClass('expanded');
      $(this).children('ol').toggle('medium');
    }
    return false;
  })
  .addClass('collapsed')
  .children('ol').hide();

  //Create the button funtionality
  $('#expandList')
  .unbind('click')
  .click(function() {
    $('.collapsed').addClass('expanded');
    $('.collapsed').children().show('medium');
  })
  $('#collapseList')
  .unbind('click')
  .click(function() {
    $('.collapsed').removeClass('expanded');
    $('.collapsed').children().hide('medium');
  });
};

.page-left-bar {
  width: 200px;
  background-color: #fff;
}

ol {
  margin-left: 0px;
  padding-left: 20px;
}

.handbook-page ol {
  color: #687074;
  counter-reset: item;
}

ol {
  counter-reset: item;
  color: #687074;
}

ol li {
  display: block;
  padding: 5px 0;
}

ol li a {
  text-decoration: none;
  color: #687074;
  padding-left: 10px;
}

ol li:before {
  content: counters(item, ".") " ";
  counter-increment: item;
  font-weight: bold;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>LIST OL  child list alignment</h1>
<div class="page-left-bar">
  <ol id='#expList'>
    <li><a href="#home">Home</a></li>
    <li><a href="#news">News</a></li>
    <li><a href="#contact">Contact</a>
      <ol>
        <li><a href="#home">Sub menu</a></li>
        <li><a href="#news">Sub menu long name</a></li>
        <li><a href="#contact">Sub menu</a></li>
        <li><a href="#about">Sub menu</a></li>
      </ol>
    </li>
    <li><a href="#about">About </a>
      <ol>
        <li><a href="#home">Mission</a></li>
        <li><a href="#news">Vision</a></li>
        <li><a href="#contact">Sub menu</a></li>
        <li><a href="#about">Sub menu</a></li>
      </ol>
    </li>
  </ol>
</div>

推荐答案

如果要切换子菜单的可见性.首先按照@MoshFeu所说,从ID中的#expList中删除#.

If you want to toggle the visibility of your submenus. First remove the # from the id #expList in your HTML as @MoshFeu said.

<ol id='expList'>

然后,您可以像这样简单地做到这一点.

Then you can simply do it like this.

$(document).ready(function(){
  $("#expList").find("ol").hide();

  $("#expList > li").click(function(){
    $(this).find("ol").slideToggle();
  });
});

请参见小提琴

这篇关于使有序列表可折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆