开发者

jQuery nth child trouble

I'm trying to wrap every set of three .item divs in a larger div, how can I do that?

Original:

<div class="item">..</div>
<div class="item">..</div>
<div class="item">..</div>
<div class="item">..</div>开发者_运维百科;
<div class="item">..</div>
<div class="item">..</div>

After jQuery:

<div class="row">
    <div class="item">..</div>
    <div class="item">..</div>
    <div class="item">..</div>
</div>
<div class="row">
    <div class="item">..</div>
    <div class="item">..</div>
    <div class="item">..</div>
</div>

I'm having trouble figuring out the nth child equation - any help?


You can do it using .slice() and .wrapAll() like this:

var divs = $("div.item");
for(var i = 0; i < divs.length; i += 3) {
  divs.slice(i, i+3).wrapAll("<div class='row'></div>");
}

You can test it here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜