简体   繁体   中英

How to select all following elements with class in jquery

How can I select all following elements from my selector, stopping when the next element is not part of it and ignoring the further elements?

example:

<div class="primary"></div>
  <div class="sub"></div>
  <div class="sub"></div>
  <div class="sub"></div>
<div class="primary"></div>
 <div class="sub"></div>
<div class="primary"></div>
 <div class="sub"></div>
 <div class="sub"></div>

now in js / jquery:

$('.primary').click(function()
{
$(this)....  //acces to all following elements with class "sub"
});

so when clicking on the first primary div, I can affect the 3 following "sub" divs I know I could do some kind of .next() in a while, but this seams inefficient.

Use .nextUntil()

$('.primary').click(function(){
    $(this).nextUntil('.primary').text($(this).text())
});

Demo: Fiddle

这将对您有所帮助

**SEE DEMO http://jsfiddle.net/dhaval17/W3Jsy/**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM