jQuery/js 将 div 附近的 类 换成单个 div

jQuery/js wrap near div with same classes into single div

我有以下结构,我需要将 div 包裹在 .item 周围。 有些地方有两个项目,有些地方只有一个项目:

<div class="section">
  <div class="heading"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="heading"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="heading"></div>
  <div class="item"></div>
  <div class="heading"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

我需要这种格式的输出:

<div class="section">
  <div class="heading"></div>
  <div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
  <div class="heading"></div>
  <div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
  <div class="heading"></div>
  <div>
    <div class="item"></div>
  </div>
  <div class="heading"></div>
  <div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
</div>

我尝试使用以下代码,但它将所有代码包装为单个 class。

var classes = {};
$(".section > div").each(function() {
    classes[$(this).attr("class")] = true;
});
for (singleClass in classes) {
    $("." + singleClass).wrapAll('<div class="item" />');
}

使用 .each() 遍历 .heading 元素,并使用 .nextUntil() 循环相邻的 select .item 元素,然后使用 .wrapAll()

$(".heading").each(function(){
  $(this).nextUntil(".heading").wrapAll("<div></div>");
});
.heading + div {background: #ccc}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section">
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="heading">heading</div>
  <div class="item">item</div>
  <div class="item">item</div>
</div>

  1. 将 nextUntil() 与 warpAll() 一起使用

$(".heading").each(function() {
  $(this).nextUntil(".heading").wrapAll("<div class='red'></div>")
});
.red {
  border: red 1px solid
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section">
  <div class="heading">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="heading">4</div>
  <div class="item">5</div>
  <div class="item">6</div>
  <div class="heading">7</div>
  <div class="item">8</div>
  <div class="heading">9</div>
  <div class="item">0</div>
  <div class="item">-</div>
</div>

尝试

let h,s= document.querySelector('.section');

[...s.children].forEach(e=>  {
  if(e.className=='heading') { 
    h=document.createElement("div")
    e.insertAdjacentElement('afterend',h)
  } else {
    h.appendChild(e)
  }
})

function change() {
  let h,s= document.querySelector('.section');

  [...s.children].forEach(e=>  {
    if(e.className=='heading') { 
      h=document.createElement("div")
      e.insertAdjacentElement('afterend',h)
    } else {
      h.appendChild(e)
    }
  })
}
div { margin-left: 30px;}
<button onclick="change()">Click here to change</button>

<div class="section">
  <div class="heading">head 1</div>
  <div class="item">item 1</div>
  <div class="item">item 2</div>
  <div class="heading">head 2</div>
  <div class="item">item 3</div>
  <div class="item">item 4</div>
  <div class="heading">head 3</div>
  <div class="item">item 5</div>
  <div class="heading">head 4</div>
  <div class="item">item 6</div>
  <div class="item">item 7</div>
</div>