jQuery展开折叠定义列表(DL)中的所有DT标签

这是我想要完成的一个例子,除了它使用列表(UL和LI): http : //homework.nwsnet.de/news/ea21_turn-nested-lists-into-a-collapsible-tree-with -jquery

我的数据使用DL,DT和DD标签构建,如下所示:

Root
Coffee
black hot drink
Milk
white cold drink
Beer
European
Heineken
Mexican
Corona

如何使用jQuery将每个DT(及其相应的DD内容)转换为可折叠/可扩展的节点,即树视图?

在最基本的情况下,您只需使用click事件处理程序toggle

 // When any dt element is clicked $('dt').click(function(e){ // All dt elements after this dt element until the next dt element // Will be hidden or shown depending on it's current visibility $(this).nextUntil('dt').toggle(); }); // Hide all dd elements to start with $('dd').hide(); 

当然,您将要使用toggleClass来适当地添加其他样式以及其他效果。 见: http : //www.jsfiddle.net/yijiang/EA4R5/1/

男人自己John Resig已经有一个video解释了如何使用DL,DT和DD标签创建这样的菜单。 查看: