如何使用jQuery仅切换列表中的某些元素?

时间:2011-10-03 15:48:04

标签: javascript jquery

我有以下HTML:

 <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&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>
</div>

当我点击节标题时,我想切换io-sidebar-link-advanced。

我有一个不起作用的jquery:

$('.io-sidebar-section-collapsed').click(function () {  
    console.log('section header clicked!');
    console.log($(this).parent());
    $(this).parent().siblings('.io-sidebar-link-advanced').toggle('fast',function(){});
});

我怎样才能让它发挥作用?

3 个答案:

答案 0 :(得分:2)

$(this).parent()指的是div,所以你选择了div的兄弟姐妹。请尝试使用$(this).parent().find(...)代替$(this).parent().siblings(...)

答案 1 :(得分:1)

    $(this).parent().siblings('.io-sidebar-link-advanced').toggle('fast',function(){});

需要

    $(this).siblings('span').children('.io-sidebar-link-advanced').toggle('fast',function(){});

父div在这里没有兄弟姐妹。

答案 2 :(得分:0)

替换:

$(this).parent().siblings('.io-sidebar-link-advanced').toggle('fast',function(){});

使用:

$(this).parent().find('.io-sidebar-link-advanced').toggle('fast',function(){});

Example