使用jQuery将不同数量的div分组到同一个类,直到“最后”类

时间:2022-09-16 07:34:35

I've seen other related questions but not quite like mine.

我见过其他相关的问题,但不太像我的。

I have blocks of code with varying number of rows and want to group those rows, including the "last" div:

我有不同行数的代码块,并希望对这些行进行分组,包括“last”div:

<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
<br>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
<br>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>

I want:

我想要:

<div class="wrapper">
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
</div>
<br>
<div class="wrapper">
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
</div>
<br>
<div class="wrapper">
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
</div>

I've tried various find, addBefore, wrapAll variations but just can't get it.

我尝试了各种find,addBefore,wrapAll变种但却无法得到它。

I've tried variations of:

我尝试过各种变化:

$(this).find(".row, .last").wrapAll('<div class="wrapper"></div>');

2 个解决方案

#1


1  

We can acheive this by combining prevAll and wrapAll. Below is the ocde

我们可以通过组合prevAll和wrapAll来实现这一点。下面是ocde

$("div.last").each(function() {
  $(this).prevAll(".row").addBack().wrapAll("<div class='wrapper' />"); 
});

Demo : https://jsfiddle.net/wx8074qn/3/

演示:https://jsfiddle.net/wx8074qn/3/

#2


3  

Here is one way:-

这是一种方式: -

//find all the 'first' .row by checking the previous is not a .row
$('.row').prev(':not(.row)').next().each(function() {
  //wrap all next elements until you find something not a div
  $(this).nextUntil(':not(div)').addBack().wrapAll($("<div></div>", {
    class: 'wrapper'
  }));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
<br>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
<br>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>

#1


1  

We can acheive this by combining prevAll and wrapAll. Below is the ocde

我们可以通过组合prevAll和wrapAll来实现这一点。下面是ocde

$("div.last").each(function() {
  $(this).prevAll(".row").addBack().wrapAll("<div class='wrapper' />"); 
});

Demo : https://jsfiddle.net/wx8074qn/3/

演示:https://jsfiddle.net/wx8074qn/3/

#2


3  

Here is one way:-

这是一种方式: -

//find all the 'first' .row by checking the previous is not a .row
$('.row').prev(':not(.row)').next().each(function() {
  //wrap all next elements until you find something not a div
  $(this).nextUntil(':not(div)').addBack().wrapAll($("<div></div>", {
    class: 'wrapper'
  }));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
<br>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>
<br>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="last"></div>