folding.js 904 B

123456789101112131415161718192021222324252627
  1. $(function() {
  2. // Find list items representing folders and
  3. // style them accordingly. Also, turn them
  4. // into links that can expand/collapse the
  5. // tree leaf.
  6. $('li > ul').each(function(i) {
  7. // Find this list's parent list item.
  8. var parent_li = $(this).parent('li');
  9. // Style the list item as folder.
  10. parent_li.addClass('folder');
  11. // Temporarily remove the list from the
  12. // parent list item, wrap the remaining
  13. // text in an anchor, then reattach it.
  14. var sub_ul = $(this).remove();
  15. parent_li.wrapInner('<a/>').find('a').click(function() {
  16. // Make the anchor toggle the leaf display.
  17. sub_ul.toggle();
  18. parent_li.toggleClass('folder-open', 'folder')
  19. });
  20. parent_li.append(sub_ul);
  21. });
  22. // Hide all lists except the outermost.
  23. $('ul ul').hide();
  24. });