开发者

jQuery expand collapse all DT tags in definition list (DL)

Here is an example of what I am trying to accomplish, except that it uses lists (UL and LI): http://homework.nwsnet.de/news/ea21_turn-nested-lists-into-a-collapsible-tree-with-jquery

My data is structured usin开发者_如何学编程g DL, DT and DD tags, like this:

<dl>
  <dt>Root</dt>
  <dd>
    <dl>
      <dt>Coffee</dt>
      <dd>black hot drink</dd>
      <dt>Milk</dt>
      <dd>white cold drink</dd>
      <dt>Beer</dt>
      <dd>
        <dl>
          <dt>European</dt>
          <dd>Heineken</dd>
          <dt>Mexican</dt>
          <dd>Corona</dd>
        </dl>
      </dd>
    </dl>
  </dd>
</dl>

How can I use jQuery to turn each DT (and its corresponding DD content) into a collapsible/expandable node, ie a treeview?


At it's most basic, you can simply use toggle with the click event handler to do this:

// 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();

You will, of course, want to use toggleClass to add additional styles as appropriately, as well as other effects. See: http://www.jsfiddle.net/yijiang/EA4R5/1/


The man himself John Resig has already a video explaning how to create such menu using DL, DT and DD tags. Check out:

  • http://15daysofjquery.com/jquery-online-movie-tutorial-by-john-resig/29/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜