jquery select siblings 'until'

前端 未结 4 2064
温柔的废话
温柔的废话 2020-12-06 11:25

I have a DOM in the form of

&l
相关标签:
4条回答
  • 2020-12-06 11:28

    jQuery 1.4 now has the .nextUntil(selector) function:

        $('div.parent').toggle(
            function() {
                $(this).nextUntil('div.parent').hide();
             },
            function() {
                $(this).nextUntil('div.parent').show();
            }
        );
    
    0 讨论(0)
  • 2020-12-06 11:29

    * See @foson answer for jquery 1.4+ *

    Check out Ben Almans until utils.

    It gives you 3 usefull methods: nextUntil, prevUntil, parentsUntil.

    0 讨论(0)
  • 2020-12-06 11:36

    I think no need for the above custom functions, JQuery supports this functionality by nextUntil(selector, filter) function, but you should add filter to only apply your script to the filtered elements not to all next elements:

    //hide all .child elements
    $('div.child').hide();
    $('div.parent').click(function() {
      //Toggle (show or hide) only .child elements until finding .parent element.
      $(this).nextUntil('div.parent', 'div.child').slideToggle('slow');
    });
    
    0 讨论(0)
  • 2020-12-06 11:47

    You can iterate through the nextAll div siblings elements until you find the following .parent, check this example:

    $('.parent').click(function() {
      $(this).nextAll('div').each(function() {
        if ($(this).is('.parent')) {
          return false; // next parent reached, stop
        }
        $(this).toggleClass('highlight');
      });
    });
    

    Markup used:

    <div class="parent">parent 1</div>
    <div class="child">child</div>
    <div class="child">child</div>
    <div class="parent">parent 2</div>
    <div class="child">child</div>
    <div class="parent">parent 3</div>
    <div class="child">child</div>
    <div class="child">child</div>
    <div class="child">child</div>
    

    ...

    0 讨论(0)
提交回复
热议问题