我该如何获得孩子们的前四个并将其包装到div中? [英] How can I get first 4 of the childrens and wrap it into the div?
本文介绍了我该如何获得孩子们的前四个并将其包装到div中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何找到父级的前4个子级并将其包装到div中,然后再封装4个,以此类推?谷歌不知道! :(
How can I find first 4 children of the parent and wrap it into div, then next 4 and so on? Google does not know! :(
从:
<div class="main">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
收件人:
<div lass="main">
<div class="pack1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div class="pack1">
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</div>
任何帮助,不胜感激.
Any help much appreciated.
我可以通过以下方式找到前4个:$('.main > div:lt(4)').wrapAll('<div class="pack1"></div>');
但是如何获得每四个??
I can find first 4 by: $('.main > div:lt(4)').wrapAll('<div class="pack1"></div>');
But how to get every 4??
Pete
推荐答案
不是最优雅的方法,但是它的工作原理.
Not the most elegant way but its working.
var sel;
while ( (sel = $('.main > div:not(.pack1)')).length > 0 )
{
sel.slice(0,4).wrapAll('<div class="pack1"></div>');
}
这里是小提琴.
在这里有该课程的柜台.我们必须使用第二类pack
来排除所有打包的div.
Here with counter for the class. We have to use a 2nd class pack
for the exclude of all packed divs.
var sel;
var count = 1;
while ( (sel = $('.main > div:not(.pack)')).length > 0 )
{
sel.slice(0,4).wrapAll('<div class="pack pack'+ count++ +'"></div>');
}
和小提琴
或者我们一起去
var sel;
var count = 1;
while ( (sel = $('.main > div:not([class^="pack"])')).length > 0 )
{
sel.slice(0,4).wrapAll('<div class="pack'+ count++ +'"></div>');
}
为此,
小提琴.
这篇关于我该如何获得孩子们的前四个并将其包装到div中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文