如何用jQuery包装内容?

问题描述 投票:2回答:1

如何使用jQuery使用占位符div来包装内容?

所以我可以这样说:

<div class="placeholder"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="placeholder"></div>
<div class="content"></div>
<div class="content"></div>
<div class="placeholder"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="placeholder"></div>

进入:

<div class="wrap">
  <div class="placeholder"></div>
  <div class="content">Abcd</div>
  <div class="content">Abcd</div>
  <div class="content">Abcd</div>
  <div class="content">Abcd</div>
</div>
<div class="wrap">
  <div class="placeholder"></div>
  <div class="content">Abcd</div>
  <div class="content">Abcd</div>
</div>
<div class="wrap">
  <div class="placeholder"></div>
  <div class="content">Abcd</div>
  <div class="content">Abcd</div>
  <div class="content">Abcd</div>
</div>
javascript jquery html css
1个回答
11
投票

尝试使用jQuery 1.4:

$('.placeholder').each(function(){
     $(this).nextUntil('.placeholder').andSelf().wrapAll('<div class="wrap" />');
});

jQuery 1.4添加了nextUntill函数

© www.soinside.com 2019 - 2024. All rights reserved.