开发者

How can I collapse elements in a list with jQuery?

I have the following list:

<div class='sidebar-listview ui-listview' data-role='listview' data-theme='c'>
      <li class='ui-li ui-li-static ui-body-c ui-li-has-icon io-sidebar-section io-sidebar-section-collapsed io-sidebar-section-advanced-toggle' id='0aa210f2-e811-4bae-aac0-649bf87fb240'>
        <img src='/document/d5308dfc-af9e-41a4-ab0f-9b5ff6c4c43e/latest' class='ui-li-icon ui-li-thumb' height='16' width='16'/>
        <div class='io-sidebar-link-text'>Activities</div>
      </li>
      <span io-sidebar-section='0aa210f2-e811-4bae-aac0-649bf87fb240'>
        <li class='ui-li ui-li-static ui-body-d ui-li-has-icon io-sidebar-link io-sidebar-link-standard' data-theme='d' io-sidebar-section='0aa210f2-e811-4bae-aac0-649bf87fb240' io-object-view='/cms?url=ui/object&object=34029fec-b949-3780-b9e6-9522413b9f2c' io-record-view='/cms?url=ui/record&a开发者_JAVA技巧mp;object=34029fec-b949-3780-b9e6-9522413b9f2c' style='display: block'>
          <img src='/document/140075b9-878e-4fc8-9ed6-ac422046accf/latest' class='ui-li-icon ui-li-thumb' height='16' width='16'/>
          <div class='io-sidebar-link-text'>Events</div>
        </li>
        <li class='ui-li ui-li-static ui-body-d ui-li-has-icon io-sidebar-link io-sidebar-link-standard' data-theme='d' io-sidebar-section='0aa210f2-e811-4bae-aac0-649bf87fb240' io-object-view='/cms?url=ui/object&object=db13a9ad-2494-34bb-8a59-cb99fd308051' io-record-view='/cms?url=ui/record&object=db13a9ad-2494-34bb-8a59-cb99fd308051' style='display: block'>
          <img src='/document/423fc17f-08b2-46bb-a1db-a5395cd63b83/latest' class='ui-li-icon ui-li-thumb' height='16' width='16'/>
          <div class='io-sidebar-link-text'>Tasks</div>
        </li>
        <li class='ui-li ui-li-static ui-body-d ui-li-has-icon io-sidebar-link io-sidebar-link-standard' data-theme='d' io-sidebar-section='0aa210f2-e811-4bae-aac0-649bf87fb240' io-object-view='/cms?url=ui/object&object=23505d2a-bf3b-11e0-8058-001ec93afa2c' io-record-view='/cms?url=ui/record&object=23505d2a-bf3b-11e0-8058-001ec93afa2c' style='display: block'>
          <img src='/document/f7cd2d4c-1bbe-4131-b1bb-b6bff1e9bea5/latest' class='ui-li-icon ui-li-thumb' height='16' width='16'/>
          <div class='io-sidebar-link-text'>Projects</div>
        </li>
        <li class='ui-li ui-li-static ui-body-d ui-li-has-icon io-sidebar-link io-sidebar-link-advanced' data-theme='d' io-sidebar-section='0aa210f2-e811-4bae-aac0-649bf87fb240' io-object-view='/cms?url=ui/object&object=5e0d648c-bf52-11e0-b7e6-001ec93afa2c' io-record-view='/cms?url=ui/record&object=5e0d648c-bf52-11e0-b7e6-001ec93afa2c' style='display: none'>
          <img src='/document/8dbc2454-a2f8-47b0-ba32-d5b8552c0160/latest' class='ui-li-icon ui-li-thumb' height='16' width='16'/>
          <div class='io-sidebar-link-text'>Milestones</div>
        </li>
      </span>

How can I catch the io-sidebar-section-collapsed click event and have it collapse everything underneath it in the span?


I think this should work:

var el = $('.io-sidebar-section-collapsed');
el.click(function() {
    el.next().toggle();
});

If you don't want to toggle the display and just to collapse, replace .toggle with .hide.

EDIT: Looking at your code again, I'm not sure if it will work, because your markup is invalid. You could simply remove the span and replace the code above with:

var el = $('.io-sidebar-section-collapsed');
el.click(function() {
    el.next().toggle();
    el.next().next().toggle();
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜